Operações do Quantificador (Visual Basic)

As operações de quantificador retornam um valor Boolean que indica se alguns ou todos os elementos em uma sequência satisfazem uma condição.

A ilustração a seguir mostra duas operações de quantificador diferentes em duas sequências de origem diferentes. A primeira operação pergunta se algum dos elementos é o caractere 'A'. A segunda operação pergunta se todos os elementos são o caractere 'A'. Ambos os métodos retornam true neste exemplo.

LINQ Quantifier Operations

Os métodos de operador de consulta padrão que realizam operações de quantificador estão listados na seção a seguir.

Métodos

Nome do método Descrição Sintaxe da Expressão de Consulta do Visual Basic Mais informações
Tudo Determina se todos os elementos em uma sequência satisfazem uma condição. Aggregate … In … Into All(…) Enumerable.All

Queryable.All
Qualquer Determina se todos os elementos em uma sequência satisfazem uma condição. Aggregate … In … Into Any() Enumerable.Any

Queryable.Any
Contém Determina se uma sequência contém um elemento especificado. Não aplicável. Enumerable.Contains

Queryable.Contains

Exemplos de sintaxe de expressão de consulta

Esses exemplos usam a Aggregate cláusula no Visual Basic como parte da condição de filtragem em uma consulta LINQ.

O exemplo a seguir usa a Aggregate cláusula e o All método de extensão para retornar de uma coleção aquelas pessoas cujos animais de estimação são todos mais velhos do que uma idade especificada.

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

O próximo exemplo usa a Aggregate cláusula e o Any método de extensão para retornar de uma coleção aquelas pessoas que têm pelo menos um animal de estimação mais antigo que uma idade especificada.

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

Confira também