LINQ中的Contains和Any有什么区别? [英] What is the difference between Contains and Any in LINQ?
本文介绍了LINQ中的Contains和Any有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
LINQ中的Contains
和Any
有什么区别?
What is the difference between Contains
and Any
in LINQ?
推荐答案
Contains
接受一个对象,Any
接受一个谓词.
Contains
takes an object, Any
takes a predicate.
你像这样使用Contains
:
listOFInts.Contains(1);
和 Any
像这样:
listOfInts.Any(i => i == 1);
listOfInts.Any(i => i % 2 == 0); // Check if any element is an Even Number
因此,如果您想检查特定条件,请使用 Any
.如果要检查元素是否存在,请使用 Contains
.
So if you want to check for a specific condition, use Any
. If you want to check for the existence of an element, use Contains
.
这篇关于LINQ中的Contains和Any有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文