數量詞作業 (Visual Basic)
數量詞作業會傳回 Boolean 值,指出序列中的部分或所有項目是否符合條件。
下圖說明兩個不同來源序列上的兩個不同數量詞作業。 第一個作業會詢問是否有任何元素為字元 'A'。 第二個作業會詢問是否所有元素都為字元 'A'。 在此範例中,這兩種方法都會傳回 true
。
下節列出執行數量詞作業的標準查詢運算子方法。
方法
方法名稱 | 描述 | Visual Basic 查詢運算式語法 | 相關資訊 |
---|---|---|---|
全部 | 判斷序列中的所有項目是否都符合條件。 | Aggregate … In … Into All(…) |
Enumerable.All Queryable.All |
任意 | 判斷序列中的任何項目是否符合條件。 | Aggregate … In … Into Any() |
Enumerable.Any Queryable.Any |
包含 | 判斷序列是否包含指定的項目。 | 不適用。 | Enumerable.Contains Queryable.Contains |
查詢運算式語法範例
這些範例會在 LINQ 查詢中使用 Visual Basic 的 Aggregate
子句,作為篩選條件的一部分。
下列範例會使用 Aggregate
子句與 All 擴充方法,從寵物都比指定年齡年長的人員集合中傳回。
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
下列範例會使用 Aggregate
子句與 Any 擴充方法,從至少有一隻寵物比指定年齡年長的人員集合中傳回。
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