Check that value exists in a Generic List of Values

PositiveGuy picture PositiveGuy · Dec 4, 2009 · Viewed 19.5k times · Source

I am trying to figure out how to check if testInt exists in all Car.SomeID in List

So:

int testInt = 10;
List<Car> myCars = GetCars();

I want to see if there is a match on 10 in any of myCards.SomeID

Answer

Marc Gravell picture Marc Gravell · Dec 4, 2009

In the more general case, with LINQ (supports any type of abstract typed list):

bool hasCar = myCars.Any(c => c.SomeID == testInt);