LINQ to EntitiesLINQ to Entities

LINQ to Entities 提供了 Language-Integrated Query (LINQ) 支援,可讓開發人員使用 Visual Basic 或 Visual C# 針對 Entity Framework 概念模型撰寫查詢。LINQ to Entities provides Language-Integrated Query (LINQ) support that enables developers to write queries against the Entity Framework conceptual model using Visual Basic or Visual C#. 針對 Entity Framework 執行的查詢是以命令樹查詢來表示,每一個查詢都會針對物件內容來執行。Queries against the Entity Framework are represented by command tree queries, which execute against the object context. LINQ to Entities 會將 Language-Integrated Queries (LINQ) 查詢轉換成命令樹查詢、針對 Entity Framework 執行查詢,並傳回 Entity Framework 和 LINQ 都可以使用的物件。LINQ to Entities converts Language-Integrated Queries (LINQ) queries to command tree queries, executes the queries against the Entity Framework, and returns objects that can be used by both the Entity Framework and LINQ. 下列是建立及執行 LINQ to Entities 查詢的程序:The following is the process for creating and executing a LINQ to Entities query:

  1. ObjectQuery<T> 建構 ObjectContext 執行個體。Construct an ObjectQuery<T> instance from ObjectContext.

  2. 使用 ObjectQuery<T> 執行個體,在 C# 或 Visual Basic 中撰寫 LINQ to Entities 查詢。Compose a LINQ to Entities query in C# or Visual Basic by using the ObjectQuery<T> instance.

  3. 將 LINQ 標準查詢運算子和運算式轉換成命令樹。Convert LINQ standard query operators and expressions to command trees.

  4. 在命令樹中會針對資料來源查詢執行。Execute the query, in command tree representation, against the data source. 執行期間於資料來源上擲回的任何例外狀況都會直接傳遞給用戶端。Any exceptions thrown on the data source during execution are passed directly up to the client.

  5. 將查詢結果傳回用戶端。Return query results back to the client.

建構 ObjectQuery 執行個體Constructing an ObjectQuery Instance

ObjectQuery<T> 泛型類別表示傳回零個或多個具型別實體之集合的查詢。The ObjectQuery<T> generic class represents a query that returns a collection of zero or more typed entities. 物件查詢通常是從現有的物件內容所建構 (而不是手動建構),而且一定會屬於該物件內容。An object query is typically constructed from an existing object context, instead of being manually constructed, and always belongs to that object context. 此內容提供了撰寫及執行查詢所需的連接和中繼資料 (Metadata) 資訊。This context provides the connection and metadata information that is required to compose and execute the query. ObjectQuery<T> 泛型類別會實作 IQueryable<T> 泛型介面,此介面的 builder 方法可累加建置 LINQ 查詢。The ObjectQuery<T> generic class implements the IQueryable<T> generic interface, whose builder methods enable LINQ queries to be incrementally built. 使用 C# var 關鍵字 (在 Visual Basic 中啟用區域型別推斷的話為 Dim),您也可以讓編譯器推斷實體類型。You can also let the compiler infer the type of entities by using the C# var keyword (Dim in Visual Basic, with local type inference enabled).

撰寫查詢Composing the Queries

實作泛型 ObjectQuery<T> 介面之 IQueryable<T> 泛型類別的執行個體會當做 LINQ to Entities 查詢的資料來源。Instances of the ObjectQuery<T> generic class, which implements the generic IQueryable<T> interface, serve as the data source for LINQ to Entities queries. 在查詢中,您可以精確地指定想要從資料來源中擷取的資訊。In a query, you specify exactly the information that you want to retrieve from the data source. 此外,查詢也可以指定該項資訊傳回之前應該如何排序、分組和成形。A query can also specify how that information should be sorted, grouped, and shaped before it is returned. 在 LINQ 中,查詢會儲存在變數內。In LINQ, a query is stored in a variable. 這個查詢變數不會採取任何動作,也不會傳回任何資料。它只會儲存查詢資訊。This query variable takes no action and returns no data; it only stores the query information. 在您建立查詢之後,必須執行該查詢以便擷取任何資料。After you create a query you must execute that query to retrieve any data.

LINQ to Entities 查詢可以使用兩個不同的語法來撰寫:查詢運算式語法和以方法為基礎的查詢語法。LINQ to Entities queries can be composed in two different syntaxes: query expression syntax and method-based query syntax. 查詢運算式語法和以方法為基礎的查詢語法都是 C# 3.0 和 Visual Basic 9.0 中的新增功能。Query expression syntax and method-based query syntax are new in C# 3.0 and Visual Basic 9.0.

如需詳細資訊,請參閱LINQ to Entities 中的查詢For more information, see Queries in LINQ to Entities.

查詢轉換Query Conversion

若要針對 Entity Framework 執行 LINQ to Entities 查詢,LINQ 查詢必須轉換成可以針對 Entity Framework 執行的命令樹表示法。To execute a LINQ to Entities query against the Entity Framework, the LINQ query must be converted to a command tree representation that can be executed against the Entity Framework.

LINQ to Entities 查詢是由 LINQ 標準查詢運算子 (如 SelectWhereGroupBy) 和運算式 (x > 10、Contact.LastName 等等) 所組成。LINQ to Entities queries are comprised of LINQ standard query operators (such as Select, Where, and GroupBy) and expressions (x > 10, Contact.LastName, and so on). LINQ 運算子並不是由類別所定義,而是類別上的方法。LINQ operators are not defined by a class, but rather are methods on a class. 在 LINQ 中,運算式可以包含 System.Linq.Expressions 命名空間內類型所允許的任何項目,以及能以 Lambda 函式表示的任何項目 (依擴充而定)。In LINQ, expressions can contain anything allowed by types within the System.Linq.Expressions namespace and, by extension, anything that can be represented in a lambda function. 這是 Entity Framework 所允許之運算式的超集,根據定義,這些運算式限制為只有資料庫上允許且 ObjectQuery<T> 支援的作業。This is a superset of the expressions that are allowed by the Entity Framework, which are by definition restricted to operations allowed on the database, and supported by ObjectQuery<T>.

在 Entity Framework 中,運算子和運算式都是由單一型別階層所代表,然後會將它們置於命令樹中。In the Entity Framework, both operators and expressions are represented by a single type hierarchy, which are then placed in a command tree. Entity Framework 會使用此命令樹來執行查詢。The command tree is used by the Entity Framework to execute the query. 如果 LINQ 查詢不能表示為命令樹,則當轉換查詢時,將會擲回例外狀況。If the LINQ query cannot be expressed as a command tree, an exception will be thrown when the query is being converted. LINQ to Entities 查詢的轉換牽涉到兩個子轉換:標準查詢運算子的轉換及運算式的轉換。The conversion of LINQ to Entities queries involves two sub-conversions: the conversion of the standard query operators, and the conversion of the expressions.

許多 LINQ 標準查詢運算子沒有 LINQ to Entities 中的有效轉譯。There are a number of LINQ standard query operators that do not have a valid translation in LINQ to Entities. 嘗試使用這些運算子將會在查詢轉譯時產生例外狀況。Attempts to use these operators will result in an exception at query translation time. 如需支援的 LINQ to Entities 運算子的清單,請參閱支援和不支援 LINQ 方法 (LINQ to Entities)For a list of supported LINQ to Entities operators, see Supported and Unsupported LINQ Methods (LINQ to Entities).

如需 LINQ to Entities 中使用標準查詢運算子的詳細資訊,請參閱LINQ to Entities 查詢中的標準查詢運算子For more information about using the standard query operators in LINQ to Entities, see Standard Query Operators in LINQ to Entities Queries.

一般來說,LINQ to Entities 中的運算式會在伺服器上評估,所以運算式的行為不需要遵循 CLR 語意。In general, expressions in LINQ to Entities are evaluated on the server, so the behavior of the expression should not be expected to follow CLR semantics. 如需詳細資訊,請參閱LINQ to Entities 查詢中的運算式For more information, see Expressions in LINQ to Entities Queries.

如需有關如何將 CLR 方法會呼叫對應到資料來源中的標準函式的資訊,請參閱標準函式對應的 CLR 方法For information about how CLR method calls are mapped to canonical functions in the data source, see CLR Method to Canonical Function Mapping.

如需如何呼叫標準、 資料庫,以及自訂函式內LINQ to EntitiesLINQ to Entities查詢,請參閱呼叫 LINQ to Entities 查詢中的函式For information about how to call canonical, database, and custom functions from within LINQ to EntitiesLINQ to Entities queries, see Calling Functions in LINQ to Entities Queries.

查詢執行Query Execution

當使用者建立 LINQ 查詢之後,會將它轉換成與 Entity Framework 相容的表示法 (命令樹的形式),然後針對資料來源執行此查詢。After the LINQ query is created by the user, it is converted to a representation that is compatible with the Entity Framework (in the form of command trees), which is then executed against the data source. 在執行查詢時,所有的查詢運算式 (或是查詢的元件) 都會在用戶端或伺服器上評估,At query execution time, all query expressions (or components of the query) are evaluated on the client or on the server. 這包括用於結果具體化或實體投影的運算式。This includes expressions that are used in result materialization or entity projections. 如需詳細資訊,請參閱查詢執行For more information, see Query Execution. 如需如何改善效能,在編譯查詢一次,然後加以執行數次使用不同的參數資訊,請參閱編譯的查詢 (LINQ to Entities)For information on how to improve performance by compiling a query once and then executing it several times with different parameters, see Compiled Queries (LINQ to Entities).

具體化Materialization

具體化是將查詢結果以 CLR 型別形式傳回用戶端的程序。Materialization is the process of returning query results back to the client as CLR types. 在 LINQ to Entities 中,絕對不會傳回查詢結果資料記錄;一定會有支援的 CLR 型別,該型別是由使用者或 Entity Framework 所定義或是由編譯器所產生 (匿名型別)。In LINQ to Entities, query results data records are never returned; there is always a backing CLR type, defined by the user or by the Entity Framework, or generated by the compiler (anonymous types). 所有物件具體化都是由 Entity Framework 執行。All object materialization is performed by the Entity Framework. 因為無法在 Entity Framework 與 CLR 之間對應而產生的任何錯誤,都將導致物件具體化期間擲回例外狀況。Any errors that result from an inability to map between the Entity Framework and the CLR will cause exceptions to be thrown during object materialization.

查詢結果通常會以下列其中一個項目的形式來傳回:Query results are usually returned as one of the following:

  • 零個或多個具型別實體物件的集合,或是概念模型中所定義的複雜類型的投影。A collection of zero or more typed entity objects or a projection of complex types defined in the conceptual model.

  • Entity FrameworkEntity Framework 所支援的 CLR 型別。CLR types that are supported by the Entity FrameworkEntity Framework.

  • 內嵌集合。Inline collections.

  • 匿名型別。Anonymous types.

如需詳細資訊,請參閱查詢結果For more information, see Query Results.

本節內容In This Section

LINQ to Entities 中的查詢Queries in LINQ to Entities

LINQ to Entities 查詢中的運算式Expressions in LINQ to Entities Queries

在 LINQ to Entities 查詢中呼叫函式Calling Functions in LINQ to Entities Queries

已編譯查詢 (LINQ to Entities)Compiled Queries (LINQ to Entities)

查詢執行Query Execution

查詢結果Query Results

LINQ to Entities 查詢中的標準查詢運算子Standard Query Operators in LINQ to Entities Queries

CLR 方法與標準函式的對應CLR Method to Canonical Function Mapping

支援和不支援的 LINQ 方法 (LINQ to Entities)Supported and Unsupported LINQ Methods (LINQ to Entities)

LINQ to Entities 中的已知問題和考量Known Issues and Considerations in LINQ to Entities

另請參閱See Also

LINQ to Entities 中的已知問題和考量Known Issues and Considerations in LINQ to Entities
LINQ (Language-Integrated Query)LINQ (Language-Integrated Query)
LINQ 和 ADO.NETLINQ and ADO.NET
ADO.NET Entity FrameworkADO.NET Entity Framework