2017-11-08 14 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

ありがとうございます。それは私の多くの労力を節約した –

関連する問題