LINQ to EntitiesLINQ to Entities

LINQ to Entities 提供语言集成查询 (LINQ) 支持,它允许开发人员使用 Visual Basic 或 Visual C# 根据实体框架概念模型编写查询。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#. 针对实体框架的查询由针对对象上下文执行的命令目录树查询表示。Queries against the Entity Framework are represented by command tree queries, which execute against the object context. LINQ to Entities 将语言集成查询 (LINQ) 查询转换为命令目录树查询,针对实体框架执行这些查询,并返回可同时由实体框架和 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. 此上下文提供了编写和执行查询所需的连接和元数据信息。This context provides the connection and metadata information that is required to compose and execute the query. ObjectQuery<T> 泛型类实现 IQueryable<T> 泛型接口,借助于该接口的生成器方法,能够以增量方式生成 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

若要针对实体框架执行 LINQ to Entities 查询,必须将 LINQ 查询转换为可针对实体框架执行的命令目录树表示形式。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. 这是实体框架允许的表达式的超集,这些表达式在定义上限于数据库所允许的操作,并且受到 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>.

在实体框架中,运算符和表达式同时由单一类型层次结构表示,这些运算符和表达式随后会放入命令目录树中。In the Entity Framework, both operators and expressions are represented by a single type hierarchy, which are then placed in a command tree. 实体框架使用命令目录树来执行此查询。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 查询之后,该查询将转换为与实体框架兼容的表示形式(采用命令目录树形式),然后针对数据源执行此查询。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 类型,该类型由用户或实体框架定义,或者由编译器生成(匿名类型)。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). 所有对象具体化均由实体框架执行。All object materialization is performed by the 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 方法至 Canonical 函数映射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(语言集成查询)LINQ (Language-Integrated Query)
LINQ 和 ADO.NETLINQ and ADO.NET
ADO.NET 实体框架ADO.NET Entity Framework