2012-02-12 4 views
0

次のコードを書く方がいいですか? フィールドフォームが空でないかどうかを確認してから、クエリに の条件を追加する必要があります、 ありがとう:)クエリーに空/ NULL制限を加えない方法

AbstractCriterion restrictions = null; 

if (model.DateFrom != null) 
    AddRestriction(ref restrictions, 
    Restrictions.Ge(Projections.Property<Invoice>(x => x.DateIn), model.DateForm)); 

if (model.DateTo != null) 
    AddRestriction(ref restrictions, 
    Restrictions.Le(Projections.Property<Invoice>(x => x.DateIn), model.DateTo)); 

if (!string.IsNullOrEmpty(model.Prop1)) 
    AddRestriction(ref restrictions, 
    Restrictions.Eq(Projections.Property<Invoice>(x => x.Prop1), model.Prop1)); 

// ... many more conditions :) 

return m_session.QueryOver<Invoice>().Where(restrictions).List(); 

答えて

1

初心者のためのrefキーワードは必要ありません。私は、これは読みやすさを犠牲にすることなく進歩して思う:

var query = session.QueryOver<Invoice>(); 

Action<object, ICriterion> addIfNonNull = (o, c) => 
              { 
               if (o != null) 
               { 
                query.And(c); 
               } 
              }; 

addIfNonNull(model.Prop1, Restrictions.Eq(Projections.Property<Invoice>(x => x.Prop1), model.Prop1)); 

など

関連する問題