Reputation: 45096
Need to check if a list contains an item with a property value of X.
Been using FirstOrDefault and comparing to null:
searchItems.FirstOrDefault(si => si.ID == 99) == null
Is there better way to do this?
I cannot get past syntax errors on Contains. Thanks.
Upvotes: 18
Views: 15111
Reputation: 2125
There are probably a few ways to do this, here's another one:
bool any = searchItems.Any(si => si.ID == 99);
Upvotes: 7