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 codition.

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, that 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.


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 quries using these quantifiers. Hence, we create 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+"  ");
            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 + "  ");

            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 + "  ");


            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 + "  ");

  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.


The following output demonstrates the quantifier operations in LINQ.

Understanding the Quantifiers in LINQ
Quantifiers in LINQ


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.

Related Topics

A Beginner’s Tutorial on WPF in C#

Everything about Tuples in C# and When to Use?

Linear Search and Binary Search in C#

Creating Jagged Arrays in C#

Learning Indexers in C#

Understanding Method Parameter Modifiers in C#

Object Initializers in C#

Examples of Static Constructors in C#

When should We Use Private Constructors?

C# Practice Questions

C# Basic Examples

Private and Static Constructors in C#

Constructors in C#

C# Arrays

C# Examples

How to Create a C# Console Application

Creating Navigation Window Application Using WPF in C#

LINQ To SQL Examples

Understanding the Concept of Nested Classes in C#

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

Examples of Extension Methods in C#

Learning All Class Members in C#

Access Modifiers in C#

C# Root Class – Object

KeyValuePair and its Applications

IEnumerable and IEnumerator Interfaces

IEqualityComparer Interface

New Features in C# 9

Generic IList Interface and its Implementation in C#

Examples of Connected and Disconnected Approach in ADO.NET

How Data Binding Works in WPF

Language-Integrated Query (LINQ) in C#

Examples of Query Operations using LINQ in C#

Join Operation using LINQ

Deferred Query Execution and Immediate Query Execution in LINQ

Leave a Reply

Your email address will not be published. Required fields are marked *