संपादित करें

इसके माध्यम से साझा किया गया


Quantifier Operations (Visual Basic)

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

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

Methods

Method Name Description Visual Basic Query Expression Syntax More Information
All Determines whether all the elements in a sequence satisfy a condition. Aggregate … In … Into All(…) Enumerable.All

Queryable.All
Any Determines whether any elements in a sequence satisfy a condition. Aggregate … In … Into Any() Enumerable.Any

Queryable.Any
Contains Determines whether a sequence contains a specified element. 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 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