Operasi Pembilang (Visual Basic)
Operasi kuantifier mengembalikan nilai Boolean yang menunjukkan apakah beberapa atau semua elemen secara berurutan memenuhi kondisi.
Ilustrasi berikut menggambarkan dua operasi pembilang 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 mengembalikan true
dalam contoh ini.
Metode operator kueri standar yang melakukan operasi pembilang tercantum dalam bagian berikut ini.
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 |
Mana pun | Menentukan apakah ada elemen yang secara berurutan memenuhi suatu kondisi. | Aggregate … In … Into Any() |
Enumerable.Any Queryable.Any |
Berisi | Menentukan apakah urutan berisi elemen tertentu. | Tidak berlaku. | Enumerable.Contains Queryable.Contains |
Contoh Sintaksis Ekspresi Kueri
Contoh-contoh ini menggunakan klausul Aggregate
dalam Visual Basic sebagai bagian dari kondisi pemfilteran dalam kueri LINQ.
Contoh berikut menggunakan klausul Aggregate
dan metode ekstensi All untuk menampilkan koleksi orang-orang yang hewan peliharaannya 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 menampilkan koleksi orang-orang 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