C# Any Method


The Any method checks whether any of the element in a sequence satisfy a specific condition or not.

If any element satisfy the condition, true is returned.

Let us see an example.

int[] arr = {5, 7, 10, 12, 15, 18, 20};

Now, using Any() method, we will check whether any of the element in the above array is greater than 10 or not.

arr.AsQueryable().All(val => val > 5);

If any of the element satisfies the condition, then True is returned.

Let us see the complete example.

Example

 Live Demo

using System;
using System.Linq;
class Demo {
   static void Main() {
      int[] arr = {5, 7, 10, 12, 15, 18, 20};
      // checking if any of the array elements are greater than 10
      bool res = arr.AsQueryable().Any(val => val > 10);
      Console.WriteLine(res);
   }
}

Output

True

Updated on: 23-Jun-2020

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements