简体   繁体   中英

LINQ method like List.Exists(predicate)?

I'm looking for LINQ method like List.Exists(predicate) .

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

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

Use the .Any method:

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

You are looking for Any :

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

It is an Extension Method for any IEnumerable<T> defined on System.Linq.Enumerable .

Check this post for more details: Linq .Any VS .Exists - Whats the difference?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM