LINQ量詞操作

量詞操作符

這些操作符返回一個布爾值,即真或當一個序列中的部分或全部元素滿足特定條件的假。

操作符

描述

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.