Quantifiziereroperationen

Quantifizierervorgänge geben einen Boolean-Wert zurück, der angibt, ob einige oder alle Elemente einer Sequenz eine Bedingung erfüllen.

Die folgende Abbildung stellt zwei verschiedene Quantifizierervorgänge zu zwei verschiedenen Quellsequenzen dar. Der erste Vorgang fragt, ob eins oder mehrere Elemente "A" sind; das Ergebnis ist true. Der zweite Vorgang fragt, ob alle Elemente "A" sind; das Ergebnis ist true.

LINQ-Quantifizierer-Vorgänge

Die Standardabfrageoperator-Methoden, die Quantifizierervorgänge ausführen, werden im folgenden Abschnitt aufgelistet.

Methoden

Methodenname

Beschreibung

C#-Abfrageausdruckssyntax

Visual Basic-Abfrageausdruckssyntax

Weitere Informationen

Alle

Bestimmt, ob alle Elemente einer Sequenz eine Bedingung erfüllen.

Nicht zutreffend.

Aggregate … In … Into All(…)

Enumerable.All<TSource>

Queryable.All<TSource>

Irgendein

Bestimmt, ob irgendein Element einer Sequenz eine Bedingung erfüllt.

Nicht zutreffend.

Aggregate … In … Into Any()

Enumerable.Any

Queryable.Any

Enthält

Bestimmt, ob eine Sequenz ein angegebenes Element enthält.

Nicht zutreffend.

Nicht zutreffend.

Enumerable.Contains

Queryable.Contains

Beispiele von Abfrageausdruckssyntax

Diese Beispiele verwenden die Aggregate-Klausel in Visual Basic als Teil der Filterbedingung in einer LINQ-Abfrage.

Das folgende Beispiel verwendet die Aggregate-Klausel und die All<TSource>-Erweiterungsmethode, um in einer Auflistung die Personen zurückzugeben, deren Haustiere älter als ein angegebenes Alter sind.

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

Das nächste Beispiel verwendet die Aggregate-Klausel und die Any-Erweiterungsmethode, um in einer Auflistung die Personen zurückzugeben, die mindestens ein Haustier besitzen, das älter als ein angegebenes Alter ist.

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

Siehe auch

Aufgaben

Gewusst wie: Dynamisches Festlegen von Prädikatfiltern zur Laufzeit (C#-Programmierhandbuch)

Gewusst wie: Abfragen von Sätzen, die eine angegebene Gruppe von Wörtern (LINQ) enthalten

Referenz

Aggregate-Klausel (Visual Basic)

System.Linq

Konzepte

Übersicht über Standardabfrageoperatoren