Quantifier Operations in LINQ
These operators return a Boolean value i.e. True or False when some or all elements within a sequence satisfy a specific condition.
Operator | Description | C# Query Expression Syntax | VB Query Expression Syntax |
---|---|---|---|
All | Returns a value ‘True’ if all elements of a sequence satisfy a predicate condition | Not Applicable | Aggregate … In … Into All(…) |
Any | Determines by searching a sequence that whether any element of the same satisfy a specified condition | Not Applicable | Aggregate … In … Into Any() |
Contains | Returns a ‘True’ value if finds that a specific element is there in a sequence if the sequence doe not contains that specific element , ‘false’ value is returned | Not Applicable | Not Applicable |
Example of All - All(Of TSource) Extension Method
VB
Module Module1 Sub Main() 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}} 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 For Each e In query Console.WriteLine("Name = {0}", e) Next Console.WriteLine(vbLf & "Press any key to continue.") Console.ReadKey() End Sub 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 End Module
When the above code in VB is compiled ad executed, it produces the following result −
Arlene Rui Press any key to continue.
Example of Any - Extension Method
VB
Module Module1 Sub Main() 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}} 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 For Each e In query Console.WriteLine("Name = {0}", e) Next Console.WriteLine(vbLf & "Press any key to continue.") Console.ReadKey() End Sub 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 End Module
When the above code in VB is compiled ad executed, it produces the following result −
Rui Press any key to continue.
linq_query_operators.htm
Advertisements
To Continue Learning Please Login
Login with Google