paparazzo
paparazzo

Reputation: 45096

LINQ Contains Based on Property

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

Answers (2)

Christoffer
Christoffer

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

Aducci
Aducci

Reputation: 26634

You can use the Any method

searchItems.Any(si => si.ID == 99)

Upvotes: 46

Related Questions