C# Program to check whether the elements of a sequence satisfy a condition or not

CsharpProgrammingServer Side Programming

Use All() Method to check whether the elements of a sequence satisfy a condition or not. Even if one of the element do not satisfy the set condition, the All() method returns False.

To set conditions, use Lambda Expressions. Below shows a condition to check whether all the elements are greater than 20 or not.

myArr.AsQueryable().All(val => val > 20);

Let us see an example.

Example

 Live Demo

using System;
using System.Linq;
class Demo {
   static void Main() {
      int[] myArr = {7, 15, 22, 30, 40};
      // checking if all the array elements are greater than 20
      bool res = myArr.AsQueryable().All(val => val > 20);
      Console.WriteLine(res);
   }
}

Output

False
raja
Updated on 23-Jun-2020 07:30:47

Advertisements