标准查询运算符概述 (C#)Standard Query Operators Overview (C#)

标准查询运算符是组成 LINQ 模式的方法。The standard query operators are the methods that form the LINQ pattern. 这些方法中的大多数都作用于序列;其中序列指其类型实现 IEnumerable<T> 接口或 IQueryable<T> 接口的对象。Most of these methods operate on sequences, where a sequence is an object whose type implements the IEnumerable<T> interface or the IQueryable<T> interface. 标准查询运算符提供包括筛选、投影、聚合、排序等在内的查询功能。The standard query operators provide query capabilities including filtering, projection, aggregation, sorting and more.

共有两组 LINQ 标准查询运算符,一组作用于类型 IEnumerable<T> 的对象,另一组作用于类型 IQueryable<T> 的对象。There are two sets of LINQ standard query operators, one that operates on objects of type IEnumerable<T> and the other that operates on objects of type IQueryable<T>. 构成每个集合的方法分别是 EnumerableQueryable 类的静态成员。The methods that make up each set are static members of the Enumerable and Queryable classes, respectively. 这些方法被定义为作为方法运行目标的类型的扩展方法They are defined as extension methods of the type that they operate on. 这意味着可以使用静态方法语法或实例方法语法来调用它们。This means that they can be called by using either static method syntax or instance method syntax.

此外,多个标准查询运算符方法作用于那些基于 IEnumerable<T>IQueryable<T> 的类型外的类型。In addition, several standard query operator methods operate on types other than those based on IEnumerable<T> or IQueryable<T>. Enumerable 类型定义了两种这样的方法,这两种方法都作用于类型 IEnumerable 的对象。The Enumerable type defines two such methods that both operate on objects of type IEnumerable. 这些方法(Cast<TResult>(IEnumerable)OfType<TResult>(IEnumerable))均允许在 LINQ 模式中查询非参数化或非泛型集合。These methods, Cast<TResult>(IEnumerable) and OfType<TResult>(IEnumerable), let you enable a non-parameterized, or non-generic, collection to be queried in the LINQ pattern. 这些方法通过创建一个强类型的对象集合来实现这一点。They do this by creating a strongly-typed collection of objects. Queryable 类定义了两种类似的方法 Cast<TResult>(IQueryable)OfType<TResult>(IQueryable),这两种方法都作用于类型 Queryable 的对象。The Queryable class defines two similar methods, Cast<TResult>(IQueryable) and OfType<TResult>(IQueryable), that operate on objects of type Queryable.

各个标准查询运算符在执行时间上有所不同,具体情况取决于它们是返回单一值还是值序列。The standard query operators differ in the timing of their execution, depending on whether they return a singleton value or a sequence of values. 返回单一实例值的这些方法(例如 AverageSum)立即执行。Those methods that return a singleton value (for example, Average and Sum) execute immediately. 返回序列的方法会延迟查询执行,并返回一个可枚举的对象。Methods that return a sequence defer the query execution and return an enumerable object.

对于在内存中集合上运行的方法(即扩展 IEnumerable<T> 的那些方法),返回的可枚举对象将捕获传递到方法的参数。In the case of the methods that operate on in-memory collections, that is, those methods that extend IEnumerable<T>, the returned enumerable object captures the arguments that were passed to the method. 在枚举该对象时,将使用查询运算符的逻辑,并返回查询结果。When that object is enumerated, the logic of the query operator is employed and the query results are returned.

与之相反,扩展 IQueryable<T> 的方法不会实现任何查询行为,但会生成一个表示要执行的查询的表达式树。In contrast, methods that extend IQueryable<T> do not implement any querying behavior, but build an expression tree that represents the query to be performed. IQueryable<T> 对象执行查询处理。The query processing is handled by the source IQueryable<T> object.

可以在一个查询中将对查询方法的调用链接在一起,这就使得查询的复杂性可能会变得不确定。Calls to query methods can be chained together in one query, which enables queries to become arbitrarily complex.

下面的代码示例演示如何使用标准查询运算符来获取有关序列的信息。The following code example demonstrates how the standard query operators can be used to obtain information about a sequence.

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   

查询表达式语法Query Expression Syntax

某些使用更频繁的标准查询运算符具有专用的 C# 和 Visual Basic 语言关键字语法,使用这些语法可以在查询 表达式中调用这些运算符。Some of the more frequently used standard query operators have dedicated C# and Visual Basic language keyword syntax that enables them to be called as part of a query expression. 有关具有专用关键字及其对应语法的标准查询运算符的详细信息,请参阅标准查询运算符的查询表达式语法 (C#)For more information about standard query operators that have dedicated keywords and their corresponding syntaxes, see Query Expression Syntax for Standard Query Operators (C#).

扩展标准查询运算符Extending the Standard Query Operators

通过创建适合于目标域或技术的特定于域的方法,可以增大标准查询运算符的集合。You can augment the set of standard query operators by creating domain-specific methods that are appropriate for your target domain or technology. 也可以用自己的实现来替换标准查询运算符,这些实现提供诸如远程计算、查询转换和优化之类的附加服务。You can also replace the standard query operators with your own implementations that provide additional services such as remote evaluation, query translation, and optimization. 有关示例,请参见 AsEnumerableSee AsEnumerable for an example.

通过以下链接可转到一些主题,这些主题基于功能提供有关各种标准查询运算符的附加信息。The following links take you to topics that provide additional information about the various standard query operators based on functionality.

对数据进行排序 (C#)Sorting Data (C#)

集运算 (C#)Set Operations (C#)

筛选数据 (C#)Filtering Data (C#)

限定符运算 (C#)Quantifier Operations (C#)

投影运算 (C#)Projection Operations (C#)

数据分区 (C#)Partitioning Data (C#)

联接运算 (C#)Join Operations (C#)

数据分组 (C#)Grouping Data (C#)

生成运算 (C#)Generation Operations (C#)

相等运算 (C#)Equality Operations (C#)

元素运算 (C#)Element Operations (C#)

转换数据类型 (C#)Converting Data Types (C#)

串联运算 (C#)Concatenation Operations (C#)

聚合运算 (C#)Aggregation Operations (C#)

请参阅See also