Compartilhar via


Quantifier Operations

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

The following illustration depicts two different quantifier operations on two different source sequences. The first operation asks if one or more of the elements are the character 'A', and the result is true. The second operation asks if all the elements are the character 'A', and the result is true.

LINQ Quantifier Operations

The standard query operator methods that perform quantifier operations are listed in the following section.

Methods

Method Name

Description

C# Query Expression Syntax

Visual Basic Query Expression Syntax

More Information

All

Determines whether all the elements in a sequence satisfy a condition.

Not applicable.

Aggregate … In … Into All(…)

Enumerable.All<TSource>

Queryable.All<TSource>

Any

Determines whether any elements in a sequence satisfy a condition.

Not applicable.

Aggregate … In … Into Any()

Enumerable.Any

Queryable.Any

Contains

Determines whether a sequence contains a specified element.

Not applicable.

Not applicable.

Enumerable.Contains

Queryable.Contains

Query Expression Syntax Examples

These examples use the Aggregate clause in Visual Basic as part of the filtering condition in a LINQ query.

The following example uses the Aggregate clause and the All<TSource> extension method to return from a collection those people whose pets are all older than a specified age.

Class Person
    Public Property Name As String 
    Public Property Pets As Pet()
End Class 

Class Pet
    Public Property Name As String 
    Public Property Age As Integer 
End Class 

Sub All()
    Dim barley As New Pet With {.Name = "Barley", .Age = 4}
    Dim boots As New Pet With {.Name = "Boots", .Age = 1}
    Dim whiskers As New Pet With {.Name = "Whiskers", .Age = 6}
    Dim bluemoon As New Pet With {.Name = "Blue Moon", .Age = 9}
    Dim daisy As New Pet With {.Name = "Daisy", .Age = 3}

    Dim charlotte As New Person With {.Name = "Charlotte", .Pets = New Pet() {barley, boots}}
    Dim arlene As New Person With {.Name = "Arlene", .Pets = New Pet() {whiskers}}
    Dim rui As New Person With {.Name = "Rui", .Pets = New Pet() {bluemoon, daisy}}

    ' Create the list of Person objects that will be queried. 
    Dim people As New System.Collections.Generic.List(Of Person)(New Person() {charlotte, arlene, rui})

    Dim query = From pers In people 
                Where (Aggregate pt In pers.Pets Into All(pt.Age > 2)) 
                Select pers.Name

    Dim sb As New System.Text.StringBuilder()
    For Each name As String In query
        sb.AppendLine(name)
    Next 

    ' Display the results.
    MsgBox(sb.ToString())

    ' This code produces the following output: 

    ' Arlene 
    ' Rui 

End Sub

The next example uses the Aggregate clause and the Any extension method to return from a collection those people who have at least one pet that is older than a specified age.

Class Person
    Public Property Name As String 
    Public Property Pets As Pet()
End Class 

Class Pet
    Public Property Name As String 
    Public Property Age As Integer 
End Class 

Sub Any()
    Dim barley As New Pet With {.Name = "Barley", .Age = 4}
    Dim boots As New Pet With {.Name = "Boots", .Age = 1}
    Dim whiskers As New Pet With {.Name = "Whiskers", .Age = 6}
    Dim bluemoon As New Pet With {.Name = "Blue Moon", .Age = 9}
    Dim daisy As New Pet With {.Name = "Daisy", .Age = 3}

    Dim charlotte As New Person With {.Name = "Charlotte", .Pets = New Pet() {barley, boots}}
    Dim arlene As New Person With {.Name = "Arlene", .Pets = New Pet() {whiskers}}
    Dim rui As New Person With {.Name = "Rui", .Pets = New Pet() {bluemoon, daisy}}

    ' Create the list of Person objects that will be queried. 
    Dim people As New System.Collections.Generic.List(Of Person)(New Person() {charlotte, arlene, rui})

    Dim query = From pers In people 
                Where (Aggregate pt In pers.Pets Into Any(pt.Age > 7)) 
                Select pers.Name

    Dim sb As New System.Text.StringBuilder()
    For Each name As String In query
        sb.AppendLine(name)
    Next 

    ' Display the results.
    MsgBox(sb.ToString())

    ' This code produces the following output: 

    ' Rui 

End Sub

See Also

Tasks

How to: Dynamically Specify Predicate Filters at Runtime (C# Programming Guide)

How to: Query for Sentences that Contain a Specified Set of Words (LINQ)

Reference

Aggregate Clause (Visual Basic)

System.Linq

Concepts

Standard Query Operators Overview