【发布时间】:2021-01-14 00:56:07
【问题描述】:
我需要在列表中添加多个条件,但我不能
List<Account> accounts = getAccount();
在我的列表之后,我需要应用多个这样的条件,我该怎么做,因为我不能在 prdicate 上创建And。这是我的代码
Func<Account, bool> predicate = X => true;
if (request.id!= 0)
{
predicate = predicate.And(giftCard => giftCard.Id == request.Id);
}
if (request.code> 0)
{
predicate = predicate.And(giftCard => giftCard.code== request.code);
}
if (request.cjn> 0)
{
predicate = predicate.And(giftCard => giftCard.cjn== request.cjn);
}
var resuly=accounts.where(predicate);
【问题讨论】:
-
var query = myList.Where(); if(x){ query=query.Where();}。List是一个容器,它没有条件或过滤器。查询由 LINQ 提供 -
只需在
Where中添加条件,如下所示:list.Where(item => item.Id == request.Id && item.Code == request.Code && ...) -
但我需要让它们有条件地说明为什么我可以将它们放在 where 中
-
@DhouhaBenAchour:我明白是什么让你感到困惑。投票决定重新开放。