IQueryable<T> 和有什么区别?和 IEnumerable ? [英] What is the difference between IQueryable<T> and IEnumerable<T>?
问题描述
IQueryable
和 IEnumerable
有什么区别?
另请参阅IQueryable 和 IEnumerable 的区别是什么 与这个问题重叠.
See also What's the difference between IQueryable and IEnumerable that overlaps with this question.
推荐答案
首先,IQueryable
extends IEnumerable
接口,所以你可以做任何事情使用普通"IEnumerable
,您还可以使用 IQueryable
.
First of all, IQueryable<T>
extends the IEnumerable<T>
interface, so anything you can do with a "plain" IEnumerable<T>
, you can also do with an IQueryable<T>
.
IEnumerable
只有一个 GetEnumerator()
方法,它返回一个 Enumerator
,你可以调用它的 MoveNext()
方法遍历 T 的序列.
IEnumerable<T>
just has a GetEnumerator()
method that returns an Enumerator<T>
for which you can call its MoveNext()
method to iterate through a sequence of T.
IQueryable
具有的 IEnumerable
没有 是两个特别的属性——一个指向 <强>查询提供程序(例如,LINQ to SQL 提供程序)和另一个指向 查询表达式 表示 IQueryable
对象作为运行时-可以被给定查询提供程序理解的可遍历抽象语法树(在大多数情况下,您不能在不抛出异常的情况下将 LINQ to SQL 表达式提供给 LINQ to Entities 提供程序).
What IQueryable<T>
has that IEnumerable<T>
doesn't are two properties in particular—one that points to a query provider (e.g., a LINQ to SQL provider) and another one pointing to a query expression representing the IQueryable<T>
object as a runtime-traversable abstract syntax tree that can be understood by the given query provider (for the most part, you can't give a LINQ to SQL expression to a LINQ to Entities provider without an exception being thrown).
表达式可以只是对象本身的常量表达式,也可以是由一组查询运算符和操作数组成的更复杂的树.查询提供者的 IQueryProvider.Execute()
或 IQueryProvider.CreateQuery()
方法通过传递给它的 Expression 调用,然后是查询分别返回结果或另一个 IQueryable
.
The expression can simply be a constant expression of the object itself or a more complex tree of a composed set of query operators and operands. The query provider's IQueryProvider.Execute()
or IQueryProvider.CreateQuery()
methods are called with an Expression passed to it, and then either a query result or another IQueryable
is returned, respectively.
这篇关于IQueryable<T> 和有什么区别?和 IEnumerable ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!