量词操作符
这些操作符返回一个布尔值,即真或当一个序列中的部分或全部元素满足特定条件的假。
操作符 | 描述 | C#查询表达式语法 | VB查询表达式语法 |
---|---|---|---|
All | 返回一个值'True',如果序列中的所有元素满足谓词条件 | 不适用 | Aggregate … In … Into All(…) |
Any | 确定通过搜索一个序列是否相同的任何元件满足规定的条件 | 不适用 | Aggregate … In … Into Any() |
Contains | 如果找到某个特定元素有一个序列返回一个“true”的值,如果序列不包含特定的元素,'false'值返回 | 不适用 | 不适用 |
All示例 - All(Tsource)扩展方法
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
当在VB上述代码被编译和执行时,它产生了以下结果:
Arlene Rui Press any key to continue.
Any例子- 扩展方法
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
当在VB上述代码被编译和执行时,它产生了以下结果:
Rui Press any key to continue.