实体框架 Where 子句中的谓词列表
本文关键字:谓词 列表 子句 框架 Where 实体 | 更新日期: 2025-01-25 10:24:39
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);