Share via


量指定子操作

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

次の図は、2 つの異なるソース シーケンスに対する 2 つの異なる量指定操作を示しています。 最初の操作は、1 つ以上の要素が文字 "A" かどうかを確認し、結果は true になります。 2 番目の操作は、すべての要素が文字 "A" かどうかを確認し、結果は true になります。

LINQ 量指定子操作

次のセクションに、量指定操作を実行する標準クエリ演算子のメソッドの一覧を示します。

メソッド

メソッド名

説明

C# のクエリ式の構文

Visual Basic のクエリ式の構文

詳細情報

すべて

シーケンス内のすべての要素が条件を満たしているかどうかを判断します。

適用できません。

Aggregate … In … Into All(…)

Enumerable.All<TSource>

Queryable.All<TSource>

Any

シーケンス内の任意の要素が条件を満たしているかどうかを判断します。

適用できません。

Aggregate … In … Into Any()

Enumerable.Any

Queryable.Any

Contains

指定した要素がシーケンスに格納されているかどうかを判断します。

適用できません。

適用できません。

Enumerable.Contains

Queryable.Contains

クエリ式の構文の例

これらの例では、LINQ クエリのフィルター処理条件の一部として、Visual Basic で Aggregate 句を使用しています。

次の例では、Aggregate 句と All<TSource> 拡張メソッドを使用して、ペットのすべてが指定した年齢よりも上である飼い主のコレクションを返します。

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

参照

処理手順

方法 : 実行時に述語フィルターを動的に指定する (C# プログラミング ガイド)

方法 : 指定された単語のセットを含む文章を照会する (LINQ)

参照

Aggregate 句 (Visual Basic)

System.Linq

概念

標準クエリ演算子の概要