我是Iquerable的新人。 我想将“selectedBus”中的所有项目与OR连接,并将“depositi”中的所有项目与OR连接。 然后我想要2谓词(谓词和谓词3)与AND运算符。
var predicate = PredicateBuilder.False<tb_pulizie>();
var predicate3 = PredicateBuilder.False<tb_pulizie>();
var result = Context.tb_pulizie.AsQueryable();
if (selectedBus != null)
{
foreach (string keyword in selectedBus)
{
string temp = keyword;
predicate = predicate.Or(p => p.ID.Contains(temp));
}
result = Context.tb_pulizie.AsExpandable().Where(predicate);
// result = result.Where(predicate);
}
if (depositi != null)
{
foreach (string keyword in depositi)
{
string temp = keyword;
predicate3 = predicate3.Or(p => p.deposito.Contains(temp));
}
result = Context.tb_pulizie.AsExpandable().Where(predicate3);
}
我该怎么办? 谢谢!
答案 0 :(得分:0)
使用具有ExpressionStarter的最新LINQKit(从NuGet获取)来自动处理And
与Or
,以及最新的C#7.0,您可以:
var predicate1 = PredicateBuilder.New<tb_pulizie>();
if (selectedBus != null)
foreach (string keyword in selectedBus)
predicate1 = predicate1.Or(p => p.ID.Contains(keyword));
var predicate2 = PredicateBuilder.New<tb_pulizie>();
if (depositi != null)
foreach (string keyword in depositi)
predicate2 = predicate2.Or(p => p.deposito.Contains(keyword));
var result = Context.tb_pulizie.AsExpandable().Where(predicate1.And(predicate2));