1

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");
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
marbel82
  • 925
  • 1
  • 18
  • 39

2 Answers2

6

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");
Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
2

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?

Community
  • 1
  • 1
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109