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
Published on 12-Sep-2018 08:35:36
Advertisements