Check that value exists in a Generic List of Values

那年仲夏 提交于 2019-12-29 07:48:50

问题


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


回答1:


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

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



回答2:


myCars.Exists(c => c.SomeID == 10);


来源:https://stackoverflow.com/questions/1848285/check-that-value-exists-in-a-generic-list-of-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!