Quantifier Operators
The quantifier operators evaluate elements of the sequence on some condition and return a boolean value to indicate that some or all elements satisfy the condition.
Operator | Description |
---|---|
All | Checks if all the elements in a sequence satisfies the specified condition |
Any | Checks if any of the elements in a sequence satisfies the specified condition |
Contains | Checks if the sequence contains a specific element |
All
The All operator evalutes each elements in the given collection on a specified condition and returns True if all the elements satisfy a condition.
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
new Student() { StudentID = 2, StudentName = "Steve", Age = 15 } ,
new Student() { StudentID = 3, StudentName = "Bill", Age = 25 } ,
new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 }
};
// checks whether all the students are teenagers
bool areAllStudentsTeenAger = studentList.All(s => s.Age > 12 && s.Age < 20);
Console.WriteLine(areAllStudentsTeenAger);
Output:
falseAny
Any checks whether any element satisfy given condition or not? In the following example, Any operation is used to check whether any student is teen ager or not.
bool isAnyStudentTeenAger = studentList.Any(s => s.age > 12 && s.age < 20);
Output:
trueQuantifier operators are Not Supported with C# query syntax.
Learn about quanifier operator - Contains in the next section.