检查项目是否属于列表 [英] check if item belong to list
本文介绍了检查项目是否属于列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
有没有一种方法可以检查项目是否属于同一类型的列表而不使用for循环,提前谢谢
解决方案
使用列表(T)。包含方法 [ ^ ]
例如:
bool b = integerList.Contains( 2 );
或者,使用无数。任何方法 [ ^ ]如果你想使用lambda表达式。
例如,如果你有一个汽车列表。
bool b = carList.Any(a = > a.Id == 1001 );
希望这会有所帮助。
Hello everyone,
Is there a method to check if an item belongs to a list of the same type without using for loop, thanks in advance
解决方案
Hi,
Use the List(T).Contains method[^]
For example:
bool b = integerList.Contains(2);
Or, use the Enumerable.Any method[^] if you want to use a lambda expression.
For example, if you have a list of cars.
bool b = carList.Any(a => a.Id == 1001);
Hope this helps.
这篇关于检查项目是否属于列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文