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

查詢是指從資料來源中擷取資料的運算式。A query is an expression that retrieves data from a data source. 查詢通常會以特定的查詢語言來表示,例如 SQL 用於關聯式資料庫,而 XQuery 用於 XML。Queries are usually expressed in a specialized query language, such as SQL for relational databases and XQuery for XML. 因此,開發人員必須針對他們所查詢的每種資料來源或資料格式,學習新的查詢語言。Therefore, developers have had to learn a new query language for each type of data source or data format that they query. Language-Integrated Query (LINQ) 提供了一種較簡單且一致的模型,可處理各種資料來源和格式的資料。Language-Integrated Query (LINQ) offers a simpler, consistent model for working with data across various kinds of data sources and formats. 在 LINQ 查詢中,您一定會使用程式設計物件。In a LINQ query, you always work with programming objects.

LINQ 查詢作業由三個動作構成:取得資料來源、建立查詢和執行查詢。A LINQ query operation consists of three actions: obtain the data source or sources, create the query, and execute the query.

實作 IEnumerable<T> 泛型介面或 IQueryable<T> 泛型介面的資料來源可以透過 LINQ 進行查詢。Data sources that implement the IEnumerable<T> generic interface or the IQueryable<T> generic interface can be queried through LINQ. 執行個體的泛型ObjectQuery<T>類別,實作泛型IQueryable<T>介面中,做為資料來源LINQ to EntitiesLINQ to Entities查詢。Instances of the generic ObjectQuery<T> class, which implements the generic IQueryable<T> interface, serve as the data source for LINQ to EntitiesLINQ to Entities queries. ObjectQuery<T> 泛型類別表示傳回零個或多個具型別物件之集合的查詢。The ObjectQuery<T> generic class represents a query that returns a collection of zero or more typed objects. 您也可以讓編譯器推斷實體的型別使用 C# 關鍵字var(Dim 在 Visual Basic 中)。You can also let the compiler infer the type of an entity by using the C# keyword var (Dim in Visual Basic).

在此查詢中,您可以精確地指定想要從資料來源中擷取的資訊。In the 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. 如果查詢傳回一連串值,查詢變數本身必須是可查詢型別。If the query returns a sequence of values, the query variable itself must be a queryable type. 這個查詢變數不會採取任何動作,也不會傳回任何資料。它只會儲存查詢資訊。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.

查詢語法Query Syntax

LINQ to EntitiesLINQ 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 的新功能,它包含一組以類似 TRANSACT-SQL 或 XQuery 的宣告式語法撰寫的子句。Query expression syntax is new in C# 3.0 and Visual Basic 9.0, and it consists of a set of clauses written in a declarative syntax similar to Transact-SQL or XQuery. 不過,.NET Framework.NET Framework Common Language Runtime (CLR) 無法讀取查詢運算式語法本身。However, the .NET Framework.NET Framework common language runtime (CLR) cannot read the query expression syntax itself. 因此,在編譯期間,查詢運算式會轉譯為 CLR 可以了解的項目:即方法呼叫。Therefore, at compile time, query expressions are translated to something that the CLR does understand: method calls. 這些方法稱為標準查詢運算子These methods are known as the standard query operators. 身為開發人員,您可以選擇使用方法語法來直接呼叫它們,而非使用查詢語法。As a developer, you have the option of calling them directly by using method syntax, instead of using query syntax. 如需詳細資訊,請參閱 LINQ 中的查詢語法及方法語法For more information, see Query Syntax and Method Syntax in LINQ.

查詢運算式語法Query Expression Syntax

查詢運算式是宣告式查詢語法。Query expressions are a declarative query syntax. 這些語法可以讓開發人員以類似 Transact-SQL 格式的高階語言撰寫查詢。This syntax enables a developer to write queries in a high-level language that is formatted similar to Transact-SQL. 透過使用查詢運算式語法,您就可以利用最少的程式碼,針對資料來源執行同樣複雜的篩選、排序和分組作業。By using query expression syntax, you can perform even complex filtering, ordering, and grouping operations on data sources with minimal code. 如需詳細資訊,基本查詢作業 (Visual Basic)For more information, Basic Query Operations (Visual Basic). 如需示範如何使用查詢運算式語法的範例,請參閱下列主題:For examples that demonstrate how to use the query expression syntax, see the following topics:

以方法為基礎的查詢語法Method-Based Query Syntax

另一種撰寫 LINQ to EntitiesLINQ to Entities 查詢的方式是使用以方法為基礎的查詢。Another way to compose LINQ to EntitiesLINQ to Entities queries is by using method-based queries. 以方法為基礎的查詢語法是一系列的 LINQ 運算子方法,並傳遞 lambda 運算式當做參數的直接方法呼叫。The method-based query syntax is a sequence of direct method calls to LINQ operator methods, passing lambda expressions as the parameters. 如需詳細資訊,請參閱 Lambda 運算式For more information, see Lambda Expressions. 如需示範如何使用以方法為基礎的語法之範例,請參閱下列主題:For examples that demonstrate how to use method-based syntax, see the following topics:

另請參閱See Also

LINQ to EntitiesLINQ to Entities
開始使用 C# 中的 LINQGetting Started with LINQ in C#
使用 Visual Basic 撰寫 LINQ 入門Getting Started with LINQ in Visual Basic
Entity Framework 合併選項和編譯的查詢Entity Framework Merge Options and Compiled Queries