-
Book Overview & Buying
-
Table Of Contents
.NET 4.0 Generics Beginner's Guide
By :
Follow the given steps:
1. Copy the following as a query to LINQPad:
string[] names = {"Sam", "Danny", "Jeff", "Erik", "Anders","Derik"};
bool z = names.Any();
bool x = names.Any(c => c.Length == 5);
z.Dump("There are some elements");
x.Dump("There is at least one element with length 5");
2. Run the query and you will get the following output:

Both x and z will be true, in this case, as the source collection (names in this case) has some elements and a couple of them have a length of 5.
Unlike Single(), Any() doesn't complain if there is no element in the source collection or any that matches the Lambda expression.
This operator allows us to check whether all the parameters in the source collection satisfy the given condition. This expression can be passed to the method as a Lambda expression:

Suppose, we have a List of integers and we want to check whether all of them are even or not. This is the best method to do these kinds of operations...
Change the font size
Change margin width
Change background colour