Bagikan melalui


Operasi Pengkuantitas (Visual Basic)

Operasi quantifier mengembalikan Boolean nilai yang menunjukkan apakah beberapa atau semua elemen dalam urutan memenuhi kondisi.

Ilustrasi berikut menggambarkan dua operasi kuantifier yang berbeda pada dua urutan sumber yang berbeda. Operasi pertama menanyakan apakah salah satu elemen adalah karakter 'A'. Operasi kedua menanyakan apakah semua elemen adalah karakter 'A'. Kedua metode menghasilkan true pada contoh ini.

Operasi Pengkualifikasi LINQ

Metode operator kueri standar yang melakukan operasi kuantifier tercantum di bagian berikut.

Metode

Nama Metode Deskripsi Sintaks Ekspresi Kueri Visual Basic Informasi Selengkapnya
Semua Menentukan apakah semua elemen dalam urutan memenuhi kondisi. Aggregate … In … Into All(…) Enumerable.All

Queryable.All
Apa saja Menentukan apakah ada elemen dalam urutan yang memenuhi kondisi. Aggregate … In … Into Any() Enumerable.Any

Queryable.Any
Berisi Menentukan apakah urutan berisi elemen tertentu. Tidak dapat diterapkan. Enumerable.Contains

Queryable.Contains

Contoh Sintaks Ekspresi Kueri

Contoh-contoh ini menggunakan Aggregate klausa di Visual Basic sebagai bagian dari kondisi pemfilteran dalam kueri LINQ.

Contoh berikut menggunakan klausul Aggregate dan metode ekstensi All untuk mengambil dari koleksi individu-individu yang memiliki hewan peliharaan yang semuanya lebih tua dari usia yang ditentukan.

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

Contoh berikutnya menggunakan klausul Aggregate dan metode ekstensi Any untuk mengembalikan orang-orang dari koleksi yang memiliki setidaknya satu hewan peliharaan yang lebih tua dari usia yang ditentukan.

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

Lihat juga