使用Linq判断列表是否存在

7

我希望检查列表中是否有任何一个项目设置了字段为true

目前我这样做:

bool isPaid = visit.Referrals.Exists(delegate(AReferral r)
                                     {
                                         return r.IsPaidVisit;
                                     });

如何使用Linq实现这个可能对一些人来说很简单,但我现在想不出来。

1个回答

8
using System.Linq;

...

bool isPaid = visit.Referrals.Any(r => r.IsPaidVisit);

但为什么要使用Linq库呢,当你可以按照以下方式操作:

bool isPaid = visit.Referrals.Exists(r => r.IsPaidVisit);

@odyodyodys 你必须使用lambda表达式或委托。我可以问一下为什么你不想使用lambda吗? - Lasse Espeholt
起初我尝试使用“from select”语句来完成它,我想知道是否也可以用这种方式完成。 - Odys
@odyodyodys 你不能这样做。至少不要重新定义 whereselect <- 这是非常非常糟糕的解决方案。 - Lasse Espeholt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接