Quantifier Operations
Quantifier operations return a Boolean value that indicates whether some or all of the elements in a sequence satisfy a condition.
The following illustration depicts two different quantifier operations on two different source sequences. The first operation asks if one or more of the elements are the character 'A', and the result is true. The second operation asks if all the elements are the character 'A', and the result is true.
The standard query operator methods that perform quantifier operations are listed in the following section.
Methods
Method Name |
Description |
C# Query Expression Syntax |
Visual Basic Query Expression Syntax |
More Information |
---|---|---|---|---|
All |
Determines whether all the elements in a sequence satisfy a condition. |
Not applicable. |
Aggregate … In … Into All(…) |
|
Any |
Determines whether any elements in a sequence satisfy a condition. |
Not applicable. |
Aggregate … In … Into Any() |
|
Contains |
Determines whether a sequence contains a specified element. |
Not applicable. |
Not applicable. |
Query Expression Syntax Examples
These examples use the Aggregate clause in Visual Basic as part of the filtering condition in a LINQ query.
The following example uses the Aggregate clause and the All<TSource> extension method to return from a collection those people whose pets are all older than a specified age.
Class Person
Private _name As String
Private _pets As Pet()
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Pets() As Pet()
Get
Return _pets
End Get
Set(ByVal value As Pet())
_pets = value
End Set
End Property
End Class
Class Pet
Private _name As String
Private _age As Integer
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Age() As Integer
Get
Return _age
End Get
Set(ByVal value As Integer)
_age = value
End Set
End Property
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
The next example uses the Aggregate clause and the Any extension method to return from a collection those people who have at least one pet that is older than a specified age.
Class Person
Private _name As String
Private _pets As Pet()
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Pets() As Pet()
Get
Return _pets
End Get
Set(ByVal value As Pet())
_pets = value
End Set
End Property
End Class
Class Pet
Private _name As String
Private _age As Integer
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Age() As Integer
Get
Return _age
End Get
Set(ByVal value As Integer)
_age = value
End Set
End Property
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
See Also
Tasks
How to: Dynamically Specify Predicate Filters at Runtime (C# Programming Guide)
How to: Query for Sentences that Contain a Specified Set of Words (LINQ)
Concepts
Standard Query Operators Overview