C#

# Understanding the Quantifiers in LINQ

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.

C#

C#

C#