共用方式為


量詞運算 (Visual Basic)

數量值作業會傳回值 Boolean ,指出序列中的部分或所有專案是否符合條件。

下圖描繪在兩個不同的來源序列上進行的兩種不同量詞運算。 第一個作業會詢問任何元素是否為字元 『A』。 第二個作業會詢問所有元素是否都是字元 『A』。 這兩種方法都會在此範例中傳回 true

LINQ 數量值作業

執行數量值作業的標準查詢運算符方法會列在下一節中。

方法

方法名稱 說明 Visual Basic 查詢表達式語法 詳細資訊
全部 判斷序列中的所有元素是否符合條件。 Aggregate … In … Into All(…) Enumerable.All

Queryable.All
任意 判斷序列中的任何元素是否滿足條件。 Aggregate … In … Into Any() Enumerable.Any

Queryable.Any
包含 判斷序列是否包含指定的元素。 不適用。 Enumerable.Contains

Queryable.Contains

查詢表達式語法範例

這些範例會使用 Aggregate Visual Basic 中的 子句作為 LINQ 查詢中篩選條件的一部分。

下列範例會使用 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

另請參閱