2017-11-08 9 views
2

필자는 필터링 데이터를 전달하는 데 사용되는 클래스가 있습니다. 필터링 옵션을 기반으로 데이터베이스에서 데이터를 검색하려고합니다. 내 FilteringDto 클래스 :SQL 쿼리에서 조건부 쿼리 연결을 사용하여 데이터베이스에서 데이터 가져 오기

public class FilteringDto 
    { 
     public int id { get; set; } 
     public string search_text { get; set; }//only for relevant table 
     public DateTime date_from { get; set; } 
     public DateTime date_to { get; set; } 

    } 

나는 cafe_table_group 테이블에서 데이터를 검색 할 수 있습니다. 내 검색어는 다음과 같습니다.

using (ISession session = SessionFactory.OpenSession) 
       { 
        using (ITransaction transaction = session.BeginTransaction()) 
        { 
         groups = session.CreateCriteria<CafeTableGroup>().List<CafeTableGroup>(); 
         if (string.IsNullOrEmpty(filters.search_text)) 
         { 
          groups = groups.Where(a => a.cafe_table_group_name.Like(filters.search_text)).ToList(); 
         } 
         if (filters.id != 0) 
         { 
          groups = groups.Where(a => a.cafe_table_group_id == filters.id).ToList(); 
         } 
         transaction.Commit(); 

        } 
       } 

여기에 문제가 있습니다. 필터링 된 데이터를 얻으려면 먼저 테이블의 모든 데이터를 검색 한 다음 조건에 따라 필터링합니다. 단일 쿼리를 사용하여 모든 데이터가 아닌 필터링 된 데이터 만 검색 할 수있는 방법이 있습니까? 미리 감사드립니다.

답변

1

코드의 문제가 .List<CafeTableGroup>();인데 인스턴스가 너무 일찍 구체화되는 원인이됩니다. 전화를 List으로 지연하십시오.

정확한 예를 사용하고 있지 않습니다. 또한 내 코드는 CreateCriteria 대신 IQueryOver을 사용합니다. 다음과 같은 코드를 사용하여이 작업을 수행 할 수 있습니다.

public IList<Table1Entity> GetList(FilterParams filterParams = null, PageParams pageParams = null) 
{ 
    IList<Table1Entity> instance = null; 

    Conjunction conjTable1 = Restrictions.Conjunction(); 
    Conjunction conjTable2 = Restrictions.Conjunction(); 

    if(filterParams == null) 
     filterParams = new FilterParams(); 

    if(!string.IsNullOrEmpty(filterParams.Date)) 
     conjTable1.Add(Restrictions.Eq(Projections.Property<Table1Entity>(x => x.Date), filterParams.Date)); 
    if(!string.IsNullOrEmpty(filterParams.FromTime)) 
     conjTable1.Add(Restrictions.Eq(Projections.Property<Table1Entity>(x => x.FromTime), filterParams.FromTime)); 
    if(!string.IsNullOrEmpty(filterParams.ToTime)) 
     conjTable1.Add(Restrictions.Eq(Projections.Property<Table1Entity>(x => x.ToTime), filterParams.ToTime)); 
    if(!string.IsNullOrEmpty(filterParams.Id)) 
     conjTable1.Add(Restrictions.Eq(Projections.Property<Table1Entity>(x => x.Id), Guid.Parse(filterParams.Id))); 

    if(!string.IsNullOrEmpty(filterParams.Pid)) 
     conjTable2.Add(Restrictions.Eq(Projections.Property<Table2Entity>(x => x.Pid), Guid.Parse(filterParams.Pid))); 

    IQueryOver<Table1Entity> query = NHSession.QueryOver<Table1Entity>() 
       .Where(conjTable1) 
       .JoinQueryOver(x => x.Table2) 
       .And(conjTable2); 
    if(pageParams != null) 
     query = query.Skip(pageParams.SkipRecords).Take(pageParams.TakeRecords); 

    instance = query.List(); 

    return instance; 
} 

이것은 또한 조인과 페이징을 구현하는 방법을 보여줍니다.

+0

나는 일을 이해하지 못했습니다. ** NHibernate **는 ** 이후에 데이터를 검색 할 것입니다 .ToList() **가 호출됩니까? –

+1

예, List를 너무 일찍 호출하고 있습니다. 통화를 연기하십시오. 샘플 코드를주의 깊게 읽으십시오. 모든 필터를 추가 할 때 끝에 List를 호출합니다. –

+0

고맙습니다. 그게 내 많은 노력을 덜어 줬어. –