次の方法で共有


量指定子操作 (Visual Basic)

量指定子操作は、シーケンス内の一部またはすべての要素が条件を満たすかどうかを示す Boolean 値を返します。

次の図は、2 つの異なるソース シーケンスに対する 2 つの異なる量指定子操作を示しています。 最初の操作では、要素のいずれかが文字 'A' であるかどうかを確認します。 2 番目の操作では、すべての要素が文字 '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

クエリ式の構文の例

これらの例では、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 拡張メソッドを使用して、指定した年齢より古いペットを少なくとも 1 匹持っているユーザーをコレクションから返します。

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

こちらも参照ください