Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

c# - What is the difference between Contains and Any in LINQ?

What is the difference between Contains and Any in LINQ?

question from:https://stackoverflow.com/questions/23526773/what-is-the-difference-between-contains-and-any-in-linq

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Contains takes an object, Any takes a predicate.

You use Contains like this:

listOFInts.Contains(1);

and Any like this:

listOfInts.Any(i => i == 1);
listOfInts.Any(i => i % 2 == 0); // Check if any element is an Even Number

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.

MSDN for Contains, Any


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...