Share via


Niceleyici İşlemleri (Visual Basic)

Niceleyici işlemleri, bir Boolean dizideki öğelerin bazılarının veya tümünün bir koşulu karşılayıp karşılamadığını belirten bir değer döndürür.

Aşağıdaki çizimde iki farklı kaynak dizisinde iki farklı niceleyici işlemi gösterilmektedir. İlk işlem, öğelerden herhangi birinin 'A' karakteri olup olmadığını sorar. İkinci işlem, tüm öğelerin 'A' karakteri olup olmadığını sorar. Her iki yöntem de bu örnekte döndürülmektedir true .

LINQ Quantifier Operations

Niceleyici işlemleri gerçekleştiren standart sorgu işleç yöntemleri aşağıdaki bölümde listelenmiştir.

Yöntemler

Yöntem Adı Açıklama Visual Basic Sorgu İfadesi Söz Dizimi Daha Fazla Bilgi
Tümünü Bir dizideki tüm öğelerin bir koşulu karşılayıp karşılamadığını belirler. Aggregate … In … Into All(…) Enumerable.All

Queryable.All
Tümü Bir dizideki öğelerin bir koşulu karşılayıp karşılamadığını belirler. Aggregate … In … Into Any() Enumerable.Any

Queryable.Any
Contains Bir dizinin belirtilen öğeyi içerip içermediğini belirler. Uygulanamaz. Enumerable.Contains

Queryable.Contains

Sorgu İfadesi Söz Dizimi Örnekleri

Bu örneklerde, LINQ sorgusundaki filtreleme koşulunun bir parçası olarak Visual Basic'teki yan tümcesi kullanılır Aggregate .

Aşağıdaki örnek, evcil hayvanlarının tümü belirtilen yaştan AggregateAll daha yaşlı olan bir koleksiyondan dönmek için yan tümcesini ve uzantı yöntemini kullanır.

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

Sonraki örnekte, belirtilen yaştan AggregateAny daha yaşlı en az bir evcil hayvana sahip olan kişilere bir koleksiyondan dönmek için yan tümcesi ve uzantı yöntemi kullanılır.

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

Ayrıca bkz.