LINQ到实体VS LINQ的对象 - 他们是一样的吗? [英] linq to entities vs linq to objects - are they the same?
问题描述
我通常使用术语实体
来表示一个业务数据对象,并在我的脑海中, LINQ到实体
和的LINQ to对象
是相同的。那是不正确的?
I usually use the term entity
to represent a business data object and in my mind, the linq to entities
and linq to objects
were the same. Is that not correct?
推荐答案
这绝对不是这样的。
LINQ到对象是的IEnumerable< T>
,让您在对象上的任意序列的执行内存查询操作。该方法接受简单的代表在必要时
LINQ-to-Objects is a set of extension methods on IEnumerable<T>
that allow you to perform in-memory query operations on arbitrary sequences of objects. The methods accept simple delegates when necessary.
LINQ到实体是具有对的IQueryable℃的设定的扩展方法LINQ提供程序; T>
。该方法建立了一个表达式树(这就是为什么代表们实际上为通过表达式来;>
s)和供应商将根据其解析建立一个SQL查询。该表达式树
LINQ-to-Entities is a LINQ provider that has a set of extension methods on IQueryable<T>
. The methods build up an expression tree (which is why delegates are actually passed as Expression<>
s), and the provider will build up a SQL query based on its parsing of that expression tree.
作为一个例子,考虑下面的查询:
As an example, consider the following queries:
var query1 = mydb.MyEntity.Select(x => x.SomeProp).Where(x => x == "Prop");
var query2 = mydb.MyEntity.Select(x => x.SomeProp).AsEnumerable().Where(x => x == "Prop");
第一个查询是将建立一个表达式树由选择和在那里的,与两个实际上lambda表达式视为 LambdaExpression
秒。在LINQ到实体提供商将它转换成SQL,无论选择和过滤器。
The first query is will build up an expression tree consisting of a select and a where, with the two lambdas actually considered as LambdaExpression
s. The LINQ-to-Entities provider will translate that into SQL that both selects and filters.
第二个查询插入一个 AsEnumerable()
,这将迫使该查询的其余部分使用LINQ到对象。在这种情况下,提供者将产生仅基于选择SQL中,从数据库中返回所有那些记录,然后过滤将出现在内存中。显然,这很可能将是慢得多。
The second query inserts an AsEnumerable()
, which will force the remainder of the query to use LINQ-to-Objects. In that case, the provider will generate SQL based on only the selection, return all those records from the database, and then the filtering will occur in-memory. Obviously, that's likely going to be much slower.
这篇关于LINQ到实体VS LINQ的对象 - 他们是一样的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!