LINQ to Entities 和LINQ to Objects 的区别

时间:2023-12-18 23:38:26

本文资料来源:http://www.codeproject.com/Articles/246861/LINQ-to-Entities-Basic-Concepts-and-Features

LINQ to Entities needs an Object Context object. The ObjectContext object is the bridge between LINQ and the database (we will explain more about ObjectContext later). LINQ to Objects don't need any intermediate LINQ provider or API.
LINQ to Entities returns data of type IQueryable<T> whereas LINQ to Objects returns data of type IEnumerable<T>.
LINQ to Entities queries are translated to SQL by way of Expression Trees, which allow them to be evaluated as a single unit, and translated to appropriate and optimal SQL Statements. LINQ to Objects queries do not need to be translated.
LINQ to Entities queries are translated to SQL calls and executed on the specified database while LINQ to Objects queries are executed in the local machine memory.
1)linq to entity 需要objectcontext对象,而Linq to object不需要。
2)linq to entity 返回的是IEnumerable<T>类型,而 linq to object返回的是IQuerable<T>类型。
3linq to entity 通过表达式树的方式被翻译为SQL,表达式树允许被看作一个简单单元,闭然后并翻译成为恰当且合适的SQl语句。而linq to object 不需要被翻译。
4)linq to entity 被转化为SQL语句,因此在特定的数据库中执行,而linq to object在本地的机器内存中执行。 还有一点,linq to sql“已死”,微软现在主要将EF作为推荐的ORM。实际上,LINQ to SQL 被认为是C#团队证明C#这门新语言成功的中间产品。

该文中的其他一些关键点:

linq to sql 是.net 3.5的一部分,提供了运行时的管理关系型数据基础。
需要注意的一点是,在处理LINQ To Entities时是LINQ的延迟执行。
标准的查询运算符在何时执行,取决于运算是返回一个单值或者序列值。某些返回单值(比如Average和Sum)立即就执行了。而方法序列值则延迟了查询的执行,并且返回了一个可枚举(enumerable )的对象。
这些方法不消耗对象数据直到查询对象被枚举。这就是延迟执行。
操作IEnumerable<T>类型的集合时,参数被立刻传递给方法,查询结果被立刻返回;
而在IQuerable<T>类型集合的扩展方法没有实施任何查询行为,但只是创建了代表查询的表达式树。查询进程被IQuerable<T>类型的源数据处理。

Linq to Entity 外键的处理被翻译成join语句。

相关文章