像List.Exists(谓词)这样的LINQ方法?

时间:2016-10-14 09:48:31

标签: c# linq

我正在寻找像List.Exists(predicate)这样的LINQ方法。

bool exists = mylist.Exists(p => p.Name == "Kamila");

bool exists = collection.??????(p => p.Name == "Kamila");

2 个答案:

答案 0 :(得分:6)

使用.Any方法:

//Will return true (and stop future iteration the moment the predicate is met)
//Otherwise false
bool exists = collection.Any(p => p.Name == "Kamila");

答案 1 :(得分:2)

您正在寻找Any

bool exists = collection.Any(p => p.Name == "Kamila");

System.Linq.Enumerable上定义的任何Extension Method都是IEnumerable<T>

查看此帖子了解详情:Linq .Any VS .Exists - Whats the difference?