私はPredicateBuilderを非常に喜んで使用してきましたが、これまでは、連結されたANDステートメントまたはORステートメントのいずれかのみを含むクエリにのみ使用していました。ここで初めて、次のようないくつかのANDステートメントとともにネストされたORステートメントのペアが必要になります。
select x from Table1 where a = 1 AND b = 2 AND (z = 1 OR y = 2)
Albahariのドキュメントを使用して、次のように式を作成しました。
Expression<Func<TdIncSearchVw, bool>> predicate =
PredicateBuilder.True<TdIncSearchVw>(); // for AND
Expression<Func<TdIncSearchVw, bool>> innerOrPredicate =
PredicateBuilder.False<TdIncSearchVw>(); // for OR
innerOrPredicate = innerOrPredicate.Or(i=> i.IncStatusInd.Equals(incStatus));
innerOrPredicate = innerOrPredicate.Or(i=> i.RqmtStatusInd.Equals(incStatus));
predicate = predicate.And(i => i.TmTec.Equals(tecTm));
predicate = predicate.And(i => i.TmsTec.Equals(series));
predicate = predicate.And(i => i.HistoryInd.Equals(historyInd));
predicate.And(innerOrPredicate);
var query = repo.GetEnumerable(predicate);
これにより、2つのOR句を完全に無視するSQLが生成されます。
select x from TdIncSearchVw
where ((this_."TM_TEC" = :p0 and this_."TMS_TEC" = :p1)
and this_."HISTORY_IND" = :p2)
次のようなORフレーズだけを使用してみた場合:
Expression<Func<TdIncSearchVw, bool>> innerOrPredicate =
PredicateBuilder.False<TdIncSearchVw>(); // for OR
innerOrPredicate = innerOrPredicate.Or(i=> i.IncStatusInd.Equals(incStatus));
innerOrPredicate = innerOrPredicate.Or(i=> i.RqmtStatusInd.Equals(incStatus));
var query = repo.GetEnumerable(innerOrPredicate);
私は次のように期待どおりにSQLを取得します:
select X from TdIncSearchVw
where (IncStatusInd = incStatus OR RqmtStatusInd = incStatus)
次のようなANDフレーズだけを使用してみた場合:
predicate = predicate.And(i => i.TmTec.Equals(tecTm));
predicate = predicate.And(i => i.TmsTec.Equals(series));
predicate = predicate.And(i => i.HistoryInd.Equals(historyInd));
var query = repo.GetEnumerable(predicate);
私は次のようなSQLを取得します:
select x from TdIncSearchVw
where ((this_."TM_TEC" = :p0 and this_."TMS_TEC" = :p1)
and this_."HISTORY_IND" = :p2)
これは最初のクエリとまったく同じです。私はとても近くにいるようです、それは私が行方不明になっているような単純なものに違いありません。誰かが私がここで間違っていることを見ることができますか?
ありがとう、
テリー