Aracılığıyla paylaş


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 İşlemleri

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 Dizilimi Daha Fazla Bilgi
Tümü Bir dizideki tüm öğelerin bir koşulu karşılayıp karşılamadığını belirler. Aggregate … In … Into All(…) Enumerable.All

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

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

Queryable.Contains

Sorgu İfade Sözdizimi Örnekleri

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

Aşağıdaki örnek, Aggregate yan tümcesini ve All uzantı yöntemini kullanarak, evcil hayvanlarının tümü belirtilen yaştan daha yaşlı olan kişileri koleksiyondan geri döner.

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 daha yaşlı en az bir evcil hayvana sahip olan kişileri bir koleksiyondan bulmak için Aggregate yan tümcesi ve Any 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 bakınız