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 查詢作業都是強型別。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. 下圖與先前討論的範例 2 類似。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.