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


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

219 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements