LINQ 查询操作中的类型关系 (C#)Type Relationships in LINQ Query Operations (C#)

若要有效编写查询,应了解完整的查询操作中的变量类型是如何全部彼此关联的。To write queries effectively, you should understand how types of the variables in a complete query operation all relate to each other. 如果了解这些关系,就能够更容易地理解文档中的 LINQLINQ 示例和代码示例。If you understand these relationships you will more easily comprehend the LINQLINQ samples and code examples in the documentation. 另外,还能了解在使用 var 隐式对变量进行类型化时的后台操作。Furthermore, you will understand what occurs behind the scenes when variables are implicitly typed by using var.

LINQLINQ 查询操作在数据源、查询本身及查询执行中是强类型化的。query operations are strongly typed in the data source, in the query itself, and in the query execution. 查询中变量的类型必须与数据源中元素的类型和 foreach 语句中迭代变量的类型兼容。The type of the variables in the query must be compatible with the type of the elements in the data source and with the type of the iteration variable in the foreach statement. 此强类型保证在编译时捕获类型错误,以便可以在用户遇到这些错误之前更正它们。This strong typing guarantees that type errors are caught at compile time when they can be corrected before users encounter them.

为了演示这些类型关系,下面的大多数示例对所有变量使用显式类型。In order to demonstrate these type relationships, most of the examples that follow use explicit typing for all variables. 最后一个示例演示在利用使用 var 的隐式类型时,如何应用相同的原则。The last example shows how the same principles apply even when you use implicit typing by using var.

不转换源数据的查询Queries that do not Transform the Source Data

下图演示不对数据执行转换的 LINQLINQ to Objects 查询操作。The following illustration shows a LINQLINQ to Objects query operation that performs no transformations on the data. 源包含一个字符串序列,查询输出也是一个字符串序列。The source contains a sequence of strings and the query output is also a sequence of strings.

关系图显示 LINQ 查询中数据类型的关系。

  1. 数据源的类型参数决定范围变量的类型。The type argument of the data source determines the type of the range variable.

  2. 所选对象的类型决定查询变量的类型。The type of the object that is selected determines the type of the query variable. 此处的 name 是一个字符串。Here name is a string. 因此,查询变量是一个 IEnumerable<string>Therefore, the query variable is an IEnumerable<string>.

  3. foreach 语句中循环访问查询变量。The query variable is iterated over in the foreach statement. 因为查询变量是一个字符串序列,所以迭代变量也是一个字符串。Because the query variable is a sequence of strings, the iteration variable is also a string.

转换源数据的查询Queries that Transform the Source Data

下图演示对数据执行简单转换的 LINQ to SQLLINQ to SQL 查询操作。The following illustration shows a LINQ to SQLLINQ to SQL query operation that performs a simple transformation on the data. 查询将一个 Customer 对象序列用作输入,并只选择结果中的 Name 属性。The query takes a sequence of Customer objects as input, and selects only the Name property in the result. 因为 Name 是一个字符串,所以查询生成一个字符串序列作为输出。Because Name is a string, the query produces a sequence of strings as output.


  1. 数据源的类型参数决定范围变量的类型。The type argument of the data source determines the type of the range variable.

  2. select 语句返回 Name 属性,而非完整的 Customer 对象。The select statement returns the Name property instead of the complete Customer object. 因为 Name 是一个字符串,所以 custNameQuery 的类型参数是 string,而非 CustomerBecause Name is a string, the type argument of custNameQuery is string, not Customer.

  3. 因为 custNameQuery 是一个字符串序列,所以 foreach 循环的迭代变量也必须是 stringBecause custNameQuery is a sequence of strings, the foreach loop's iteration variable must also be a string.

下图演示稍微复杂的转换。The following illustration shows a slightly more complex transformation. select 语句返回只捕获原始 Customer 对象的两个成员的匿名类型。The select statement returns an anonymous type that captures just two members of the original Customer object.


  1. 数据源的类型参数始终为查询中范围变量的类型。The type argument of the data source is always the type of the range variable in the query.

  2. 因为 select 语句生成匿名类型,所以必须使用 var 隐式类型化查询变量。Because the select statement produces an anonymous type, the query variable must be implicitly typed by using var.

  3. 因为查询变量的类型是隐式的,所以 foreach 循环中的迭代变量也必须是隐式的。Because the type of the query variable is implicit, the iteration variable in the foreach loop must also be implicit.

让编译器推断类型信息Letting the compiler infer type information

虽然需要了解查询操作中的类型关系,但是也可以选择让编译器执行全部工作。Although you should understand the type relationships in a query operation, you have the option to let the compiler do all the work for you. 关键字 var 可用于查询操作中的任何本地变量。The keyword var can be used for any local variable in a query operation. 下图与前面讨论的第二个示例相似。The following illustration is similar to example number 2 that was discussed earlier. 但是,编译器为查询操作中的各个变量提供强类型。However, the compiler supplies the strong type for each variable in the query operation.


有关 var 的详细信息,请参阅隐式类型本地变量For more information about var, see Implicitly Typed Local Variables.