Saturday, March 23, 2013

Difference between All and Any operator in linq

In this post we are going to learn about difference between ‘All’ and ‘Any’ operator in Linq.

Difference between ‘All’ and ‘Any’ operator:


All operator checks whether all elements have met specific conditions or not while Any operator check whether there is any elements exist in collection or not?

So what we are waiting for Let’s take a example.
using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] intArray = {1,2,3,4,5};
            bool result = intArray.All(i => i > 2);
            Console.WriteLine(result);
            result = intArray.Any();
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}
In the above example you can see that I have created an integer array and then checked with both all and any operator. In first condition I have checked that whether elements are greater then 2 or not and same way I have checked with any operator that its contains any element or not?

Now let’s run that example. To see how its works.

Diffrence between any and all operator in linq

As you can see first condition returns false and this is true because the first element 1 is not greater then 2 so its true and second condition return true because its contains element.

That’s it. Hope you like. Stay tuned for more..
Share:

0 comments:

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews