实体框架 Where 子句中的谓词列表


List<System.Linq.Expressions.Expression<Func<MyType, bool>>> lstPredicates = new List<System.Linq.Expressions.Expression<Func<MyType, bool>>>();
foreach (MyType myAccount in lstMyType)
{
    System.Linq.Expressions.Expression<Func<MyType, bool>> predicate = 
        t => t.Account == myAccount.Account && t.Branch == myAccount.Branch;
    lstPredicates.Add(predicate);
}
lstTransactions = Context.MyTrans
   .Where(lstPredicates)
   .ToList();

我尝试只在表中运行一次MyTrans查找,因此我正在构建谓词列表。我想在交易中存在任何帐户和分支组合的列表中检索事务。

即我正在尝试生成一个谓词,例如

predicate = t => 
    (t.Account == 123 && t.Branch == London) 
    || (t.Account == 433 && t.Branch == Manchester)
    ||...
你可以

使用 Linqkit 库。使用PredicateBuilder可以执行以下操作:

 var predicate = PredicateBuilder.False<MyType>();
 foreach (MyType myAccount in lstMyType)
 {
   predicate = predicate.Or (
        t => t.Account == myAccount.Account && t.Branch == myAccount.Branch);
 }
var query= Context.MyTrans.AsExpandable().Where(predicate);

最新更新