C#idiomatic C#
quantifier-operationscsharp/linq/standard-query-operators

LINQ

Quantifier operations in LINQ

Learn about quantifier operations in LINQ. These methods, `All`, `Any`, and `Contains`, return a Boolean value indicating whether some or all elements in a sequence satisfy a condition.

  • page csharp/linq/standard-query-operators/quantifier-operations
  • updated 05/29/2024

Quantifier operations return a System.Boolean value that indicates whether some or all of the elements in a sequence satisfy a condition.


author: BillWagner ms.author: wiwagn ms.topic: include ms.date: 05/29/2024

The following illustration depicts two different quantifier operations on two different source sequences. The first operation asks if any of the elements are the character 'A'. The second operation asks if all the elements are the character 'A'. Both methods return true in this example.

LINQ Quantifier Operations

Method Name Description C# Query Expression Syntax More Information
All Determines whether all the elements in a sequence satisfy a condition. Not applicable. System.Linq.Enumerable.All
System.Linq.Queryable.All
Any Determines whether any elements in a sequence satisfy a condition. Not applicable. System.Linq.Enumerable.Any
System.Linq.Queryable.Any
Contains Determines whether a sequence contains a specified element. Not applicable. System.Linq.Enumerable.Contains
System.Linq.Queryable.Contains

All

The following example uses the All to find students that scored above 70 on all exams.


author: '' ms.author: '' ms.topic: include ms.date: 08/11/2025

sample · csharpdocs/csharp/linq/standard-query-operators/snippets/standard-query-operators/QuantifierExamples.csAllQuantifier

csharp
using System.Diagnostics;
using System.Security.Claims;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace StandardQueryOperators;
public class QuantifierExamples
{
    private static readonly IEnumerable<Student> students = Sources.Students;

    public static void RunAllSnippets()
    {
        Console.WriteLine("All:");
        AllExample();
        Console.WriteLine("Any:");
        AnyExample();
        Console.WriteLine("Contains:");
        ContainsExample();
    }

    private static void AllExample()
    {
        // <AllQuantifier>
        IEnumerable<string> names = from student in students
                                    where student.Scores.All(score => score > 70)
                                    select $"{student.FirstName} {student.LastName}: {string.Join(", ", student.Scores.Select(s => s.ToString()))}";

        foreach (string name in names)
        {
            Console.WriteLine($"{name}");
        }

        // This code produces the following output:
        //
        // Cesar Garcia: 71, 86, 77, 97
        // Nancy Engström: 75, 73, 78, 83
        // Ifunanya Ugomma: 84, 82, 96, 80
        // </AllQuantifier>
    }

    private static void AnyExample()
    {
        // <AnyQuantifier>
        IEnumerable<string> names = from student in students
                                    where student.Scores.Any(score => score > 95)
                                    select $"{student.FirstName} {student.LastName}: {student.Scores.Max()}";

        foreach (string name in names)
        {
            Console.WriteLine($"{name}");
        }

        // This code produces the following output:
        //
        // Svetlana Omelchenko: 97
        // Cesar Garcia: 97
        // Debra Garcia: 96
        // Ifeanacho Jamuike: 98
        // Ifunanya Ugomma: 96
        // Michelle Caruana: 97
        // Nwanneka Ifeoma: 98
        // Martina Mattsson: 96
        // Anastasiya Sazonova: 96
        // Jesper Jakobsson: 98
        // Max Lindgren: 96
        // </AnyQuantifier>
    }

    private static void ContainsExample()
    {
        // <ContainsQuantifier>
        IEnumerable<string> names = from student in students
                                    where student.Scores.Contains(95)
                                    select $"{student.FirstName} {student.LastName}: {string.Join(", ", student.Scores.Select(s => s.ToString()))}";

        foreach (string name in names)
        {
            Console.WriteLine($"{name}");
        }

        // This code produces the following output:
        //
        // Claire O'Donnell: 56, 78, 95, 95
        // Donald Urquhart: 92, 90, 95, 57
        // </ContainsQuantifier>
    }
}

Any

The following example uses the Any to find students that scored greater than 95 on any exam.

sample · csharpdocs/csharp/linq/standard-query-operators/snippets/standard-query-operators/QuantifierExamples.csAnyQuantifier

csharp
using System.Diagnostics;
using System.Security.Claims;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace StandardQueryOperators;
public class QuantifierExamples
{
    private static readonly IEnumerable<Student> students = Sources.Students;

    public static void RunAllSnippets()
    {
        Console.WriteLine("All:");
        AllExample();
        Console.WriteLine("Any:");
        AnyExample();
        Console.WriteLine("Contains:");
        ContainsExample();
    }

    private static void AllExample()
    {
        // <AllQuantifier>
        IEnumerable<string> names = from student in students
                                    where student.Scores.All(score => score > 70)
                                    select $"{student.FirstName} {student.LastName}: {string.Join(", ", student.Scores.Select(s => s.ToString()))}";

        foreach (string name in names)
        {
            Console.WriteLine($"{name}");
        }

        // This code produces the following output:
        //
        // Cesar Garcia: 71, 86, 77, 97
        // Nancy Engström: 75, 73, 78, 83
        // Ifunanya Ugomma: 84, 82, 96, 80
        // </AllQuantifier>
    }

    private static void AnyExample()
    {
        // <AnyQuantifier>
        IEnumerable<string> names = from student in students
                                    where student.Scores.Any(score => score > 95)
                                    select $"{student.FirstName} {student.LastName}: {student.Scores.Max()}";

        foreach (string name in names)
        {
            Console.WriteLine($"{name}");
        }

        // This code produces the following output:
        //
        // Svetlana Omelchenko: 97
        // Cesar Garcia: 97
        // Debra Garcia: 96
        // Ifeanacho Jamuike: 98
        // Ifunanya Ugomma: 96
        // Michelle Caruana: 97
        // Nwanneka Ifeoma: 98
        // Martina Mattsson: 96
        // Anastasiya Sazonova: 96
        // Jesper Jakobsson: 98
        // Max Lindgren: 96
        // </AnyQuantifier>
    }

    private static void ContainsExample()
    {
        // <ContainsQuantifier>
        IEnumerable<string> names = from student in students
                                    where student.Scores.Contains(95)
                                    select $"{student.FirstName} {student.LastName}: {string.Join(", ", student.Scores.Select(s => s.ToString()))}";

        foreach (string name in names)
        {
            Console.WriteLine($"{name}");
        }

        // This code produces the following output:
        //
        // Claire O'Donnell: 56, 78, 95, 95
        // Donald Urquhart: 92, 90, 95, 57
        // </ContainsQuantifier>
    }
}

Contains

The following example uses the Contains to find students that scored exactly 95 on an exam.

sample · csharpdocs/csharp/linq/standard-query-operators/snippets/standard-query-operators/QuantifierExamples.csContainsQuantifier

csharp
using System.Diagnostics;
using System.Security.Claims;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace StandardQueryOperators;
public class QuantifierExamples
{
    private static readonly IEnumerable<Student> students = Sources.Students;

    public static void RunAllSnippets()
    {
        Console.WriteLine("All:");
        AllExample();
        Console.WriteLine("Any:");
        AnyExample();
        Console.WriteLine("Contains:");
        ContainsExample();
    }

    private static void AllExample()
    {
        // <AllQuantifier>
        IEnumerable<string> names = from student in students
                                    where student.Scores.All(score => score > 70)
                                    select $"{student.FirstName} {student.LastName}: {string.Join(", ", student.Scores.Select(s => s.ToString()))}";

        foreach (string name in names)
        {
            Console.WriteLine($"{name}");
        }

        // This code produces the following output:
        //
        // Cesar Garcia: 71, 86, 77, 97
        // Nancy Engström: 75, 73, 78, 83
        // Ifunanya Ugomma: 84, 82, 96, 80
        // </AllQuantifier>
    }

    private static void AnyExample()
    {
        // <AnyQuantifier>
        IEnumerable<string> names = from student in students
                                    where student.Scores.Any(score => score > 95)
                                    select $"{student.FirstName} {student.LastName}: {student.Scores.Max()}";

        foreach (string name in names)
        {
            Console.WriteLine($"{name}");
        }

        // This code produces the following output:
        //
        // Svetlana Omelchenko: 97
        // Cesar Garcia: 97
        // Debra Garcia: 96
        // Ifeanacho Jamuike: 98
        // Ifunanya Ugomma: 96
        // Michelle Caruana: 97
        // Nwanneka Ifeoma: 98
        // Martina Mattsson: 96
        // Anastasiya Sazonova: 96
        // Jesper Jakobsson: 98
        // Max Lindgren: 96
        // </AnyQuantifier>
    }

    private static void ContainsExample()
    {
        // <ContainsQuantifier>
        IEnumerable<string> names = from student in students
                                    where student.Scores.Contains(95)
                                    select $"{student.FirstName} {student.LastName}: {string.Join(", ", student.Scores.Select(s => s.ToString()))}";

        foreach (string name in names)
        {
            Console.WriteLine($"{name}");
        }

        // This code produces the following output:
        //
        // Claire O'Donnell: 56, 78, 95, 95
        // Donald Urquhart: 92, 90, 95, 57
        // </ContainsQuantifier>
    }
}

See also