Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check that value exists in a Generic List of Values

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

like image 298
PositiveGuy Avatar asked Dec 04 '09 16:12

PositiveGuy


People also ask

How to check value exist in list c#?

List < string > list1 = new List < string > () { "Lawrence", "Adams", "Pitt", "Tom" }; Now use the Contains method to check if an item exits in a list or not.

How do you check if a object is present in a list?

bool containsItem = myList. Any(item => item. UniqueProperty == wonderIfItsPresent. UniqueProperty);


1 Answers

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

bool hasCar = myCars.Any(c => c.SomeID == testInt);
like image 58
Marc Gravell Avatar answered Sep 23 '22 02:09

Marc Gravell