【vs.net 2008系列-LINQ】 10.限定符操作

限定符运算返回一个 Boolean 值,该值指示序列中是否有一些元素满足条件或是否所有元素都满足条件。

下图描述了两个不同源序列上的两个不同限定符运算。第一个运算询问是否有一个或多个元素为字符“A”,结果为 true。第二个运算询问是否所有元素都为字符“A”,结果为 true

LINQ 限定符操作

以下部分列出了执行限定符运算的标准查询运算符方法。

方法名

说明

C# 查询表达式语法

Visual Basic 查询表达式语法

更多信息

All

确定是否序列中的所有元素都满足条件。

不适用。

Aggregate … In … Into All(…)

Enumerable..::.All<(Of <(TSource>)>)

Queryable..::.All<(Of <(TSource>)>)

Any

确定序列中是否有元素满足条件。

不适用。

Aggregate … In … Into Any()

Enumerable..::.Any

Queryable..::.Any

Contains

确定序列是否包含指定的元素。

不适用。

不适用。

Enumerable..::.Contains

Queryable..::.Contains

下面这些示例将 Visual Basic 中的 Aggregate 子句用作 LINQ 查询中的筛选条件的一部分。

第一个示例使用 Aggregate 子句和 All<(Of <(TSource>)>) 扩展方法从集合中返回其宠物的年龄全都大于指定年龄的人员。

Visual Basic
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
            

第二个示例使用 Aggregate 子句和 Any 扩展方法从集合中返回那些至少有一只宠物的年龄大于指定年龄的人员。

Visual Basic
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
            

 

本文出处MSDN http://msdn.microsoft.com/zh-cn/library/bb546128.aspx

posted on 2008-10-22 10:09  黄昌楠  阅读(253)  评论(0)    收藏  举报

导航