C#

# Programmingempire

In this post on Understanding the Quantifiers in LINQ, I will explain the three quantifier operators – Any, All, and Contains. Basically, we use quantifiers when we need to determine how many elements in a sequence satisfy a particular condition.

Nevertheless, we encounter situations where we need to seek whether all members of the sequence satisfy a certain condition or only some of the members satisfy it. Consequently, we have the quantifiers all and any.

Besides all and any, we have another quantifier called Contains, which determines whether a specific element is present in a sequence or not.

The following quantifiers are available in C# as a part of Language-Integrated Query (LINQ). Additionally, these quantifiers return a boolean value.

• Any
• All
• Contains

### The Any Quantifier

Whenever we need to determine that whether any member of the sequence satisfies the given condition we can use any quantifier.

### The All Quantifier

In a similar manner, the all quantifier returns a boolean value true when all members of the sequence satisfy the given condition. Otherwise, it returns false.

### Contains

Lastly, the Contains quantifier determines the presence of a member in the sequence. In other words, it returns a true value if the given element is present in the sequence. Otherwise, it returns a false value.

### Example Code for Understanding the Quantifiers in LINQ

As an illustration, consider the following example that determines certain information from the array of the objects of class Student. For this purpose, let us first define a class Student with the following properties – the name of the student, and the marks in three subjects using an array of integers.

``````class Student
{
public string sname { get; set; }
public int[] marks { get; set; }
}``````

After that, create an array of objects if the class Student as shown below.

``````Student[] student_array = new Student[]
{
new Student{sname="A", marks=new int[]{56, 89, 67 } },
new Student{sname="B", marks=new int[]{30, 18, 27 } },
new Student{sname="C", marks=new int[]{96, 88, 90 } },
new Student{sname="D", marks=new int[]{88, 77, 87 } },
new Student{sname="E", marks=new int[]{67, 44, 89 } },
new Student{sname="F", marks=new int[]{44, 32, 53 } },
new Student{sname="G", marks=new int[]{48, 22, 89 } },
new Student{sname="H", marks=new int[]{34, 77, 35 } },
new Student{sname="I", marks=new int[]{55, 67, 67 } },
new Student{sname="J", marks=new int[]{57, 26, 75 } },
new Student{sname="K", marks=new int[]{59, 46, 38 } },
new Student{sname="L", marks=new int[]{78, 49, 88 } },
new Student{sname="M", marks=new int[]{8, 12, 16 } }
};``````

Finally, write the queries using these quantifiers. Hence, we create the following queries.

``````IEnumerable<string> students = from s in student_array
where s.marks.All(m => m >= 75)
select s.sname;
foreach (string s in students)
Console.Write(s+"  ");
Console.WriteLine();
IEnumerable<string> q1 = from s in student_array
where s.marks.Any(m => m >= 75)
select s.sname;
foreach (string s in q1)
Console.Write(s + "  ");
Console.WriteLine();

IEnumerable<string> q2 = from s in student_array
where s.marks.Any(m => m <30)
select s.sname;
foreach (string s in q2)
Console.Write(s + "  ");
Console.WriteLine();

Console.WriteLine();

IEnumerable<string> q3 = from s in student_array
where s.marks.All(m => m < 30)
select s.sname;
foreach (string s in q3)
Console.Write(s + "  ");

Console.WriteLine();``````
1. In fact, the first query determines whether a student has got more than 75 marks in all subjects.
2. Secondly, the query determines whether a student has got more than 75 marks in any subject.
3. Likewise, the third query determines whether a student has got marks less than 30 in any subject.
4. Finally, the last query determines students who’ve got less than 30 marks in all subjects.

Output

The following output demonstrates the quantifier operations in LINQ.

### Summary

To sum up, this article on Understanding the Quantifiers in LINQ discusses the three quantifiers (all, any, and contains) and demonstrates their use in writing queries using LINQ.

Selection Sort in C#

Insertion Sort in C#

Bubble Sort in C#

How to Create Instance Variables and Class Variables in Python

Comparing Rows of Two Tables with ADO.NET

Example of Label and Textbox Control in ASP.NET

One Dimensional and Two Dimensuonal Indexers in C#

Private and Static Constructors in C#

Methods of Array Class

Anonymous Functions in C#

Programs to Find Armstrong Numbers in C#

Matrix Multiplication in C#

One Dimensional and Two Dimensional Indexers in C#

Static Class Example in C#

Rotating an Array in C#

Generic IList Interface and its Implementation in C#

Recursive Binary search in C#

C# Practice Questions

Creating Navigation Window Application Using WPF in C#

Find Intersection Using Arrays

An array of Objects and Object Initializer

Performing Set Operations in LINQ

Using Quantifiers in LINQ

Data Binding Using BulletedList Control

Grouping Queries in LINQ

Generic Binary Search in C#

Understanding the Quantifiers in LINQ

Join Operation using LINQ

Deferred Query Execution and Immediate Query Execution in LINQ

Examples of Query Operations using LINQ in C#

An array of Objects and Object Initializer

Language-Integrated Query (LINQ) in C#

How Data Binding Works in WPF

Examples of Connected and Disconnected Approach in ADO.NET

New Features in C# 9

IEnumerable and IEnumerator Interfaces

KeyValuePair and its Applications

C# Root Class – Object

Access Modifiers in C#

Learning Properties in C#

Learning All Class Members in C#

Examples of Extension Methods in C#

How to Setup a Connection with SQL Server Database in Visual Studio

Understanding the Concept of Nested Classes in C#

LINQ To SQL Examples

A Beginner’s Tutorial on WPF in C#

Explaining C# Records with Examples

Everything about Tuples in C# and When to Use?

Creating Jagged Arrays in C#

Linear Search and Binary search in C#

Learning Indexers in C#

Object Initializers in C#

Examples of Static Constructors in C#

When should We Use Private Constructors?

C# Basic Examples

IEqualityComparer Interface

C#

C#

C#