Share via


标准查询运算符概述

“标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法。 大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了 IEnumerable<T> 接口或 IQueryable<T> 接口。 标准查询运算符提供了包括筛选、投影、聚合、排序等功能在内的查询功能。

共有两组 LINQ 标准查询运算符,一组在类型为 IEnumerable<T> 的对象上运行,另一组在类型为 IQueryable<T> 的对象上运行。 构成每组运算符的方法分别是 EnumerableQueryable 类的静态成员。 这些方法被定义为作为方法运行目标的类型的“扩展方法”。 这意味着可以使用静态方法语法或实例方法语法来调用它们。

此外,许多标准查询运算符方法运行所针对的类型不是基于 IEnumerable<T>IQueryable<T> 的类型。 Enumerable 类型定义两个此类方法,这些方法都在类型为 IEnumerable 的对象上运行。 利用这些方法(Cast<TResult>(IEnumerable)OfType<TResult>(IEnumerable)),您将能够在 LINQ 模式中查询非参数化或非泛型集合。 这些方法通过创建一个强类型的对象集合来实现这一点。 Queryable 类定义两个类似的方法(Cast<TResult>(IQueryable)OfType<TResult>(IQueryable)),这些方法在类型为 Queryable 的对象上运行。

各个标准查询运算符在执行时间上有所不同,具体情况取决于它们是返回单一值还是值序列。 返回单一值的方法(例如 AverageSum)会立即执行。 返回序列的方法会延迟查询执行,并返回一个可枚举的对象。

对于在内存中集合上运行的方法(即扩展 IEnumerable<T> 的那些方法),返回的可枚举对象将捕获传递到方法的参数。 在枚举该对象时,将使用查询运算符的逻辑,并返回查询结果。

与之相反,扩展 IQueryable<T> 的方法不会实现任何查询行为,但会生成一个表示要执行的查询的表达式树。 查询处理由源 IQueryable<T> 对象处理。

可以在一个查询中将对查询方法的调用链接在一起,这就使得查询的复杂性可能会变得不确定。

下面的代码示例演示如何使用标准查询运算符来获取有关序列的信息。

        Dim sentence = "the quick brown fox jumps over the lazy dog"
        ' Split the string into individual words to create a collection.
        Dim words = sentence.Split(" "c)

        Dim query = From word In words 
                    Group word.ToUpper() By word.Length Into gr = Group 
                    Order By Length _
                    Select Length, GroupedWords = gr

        Dim output As New System.Text.StringBuilder
        For Each obj In query
            output.AppendLine(String.Format("Words of length {0}:", obj.Length))
            For Each word As String In obj.GroupedWords
                output.AppendLine(word)
            Next
        Next

        'Display the output
        MsgBox(output.ToString())

        ' This code example produces the following output:
        '
        ' Words of length 3:
        ' THE
        ' FOX
        ' THE
        ' DOG
        ' Words of length 4:
        ' OVER
        ' LAZY
        ' Words of length 5:
        ' QUICK
        ' BROWN
        ' JUMPS 

           string sentence = "the quick brown fox jumps over the lazy dog";
           // Split the string into individual words to create a collection.
           string[] words = sentence.Split(' ');

           // Using query expression syntax.
           var query = from word in words
                       group word.ToUpper() by word.Length into gr
                       orderby gr.Key
                       select new { Length = gr.Key, Words = gr };

           // Using method-based query syntax.
           var query2 = words.
               GroupBy(w => w.Length, w => w.ToUpper()).
               Select(g => new { Length = g.Key, Words = g }).
               OrderBy(o => o.Length);

           foreach (var obj in query)
           {
               Console.WriteLine("Words of length {0}:", obj.Length);
               foreach (string word in obj.Words)
                   Console.WriteLine(word);
           }

           // This code example produces the following output:
           //
           // Words of length 3:
           // THE
           // FOX
           // THE
           // DOG
           // Words of length 4:
           // OVER
           // LAZY
           // Words of length 5:
           // QUICK
           // BROWN
           // JUMPS 

查询表达式语法

某些使用更频繁的标准查询运算符具有专用的 C# 和 Visual Basic 语言关键字语法,使用这些语法可以在“查询表达式”中调用这些运算符。 有关具有专用关键字及其对应语法的标准查询运算符的更多信息,请参见标准查询运算符的查询表达式语法

扩展标准查询运算符

通过创建适合于目标域或技术的特定于域的方法,您可以增大标准查询运算符的集合。 也可以用您自己的实现来替换标准查询运算符,这些实现提供诸如远程计算、查询转换和优化等附加服务。 有关示例,请参见 AsEnumerable<TSource>

相关章节

通过以下链接可转到一些主题,这些主题基于功能提供有关各种标准查询运算符的附加信息。

对数据进行排序

Set 运算

筛选数据

限定符操作

投影运算

数据分区

联接运算

数据分组

生成操作

相等运算

元素操作

转换数据类型

串联运算

聚合操作

请参见

参考

Enumerable

Queryable

扩展方法(C# 编程指南)

概念

LINQ 查询简介 (C#)

标准查询运算符的查询表达式语法

标准查询运算符按执行方式的分类

扩展方法 (Visual Basic)