之前已经说了很多,但回到根源,以一种更专业的方式:
IEnumerable是一个在内存中可以枚举的对象的集合——一个在内存中的序列,使得迭代成为可能(使得在每一个循环中都很容易,尽管你可以只使用IEnumerator)。它们就这样存在于记忆中。
IQueryable是一个表达式树,它将在某个时刻被转换成其他东西,并具有枚举最终结果的能力。我想这就是大多数人困惑的地方。
它们显然有着不同的内涵。
IQueryable表示一个表达式树(简单地说,就是一个查询),一旦调用了发布api,它就会被底层查询提供者转换成其他东西,比如LINQ聚合函数(Sum, Count等)或ToList[数组,字典,…]。IQueryable对象也实现了IEnumerable, IEnumerable<T>,因此如果它们表示一个查询,那么该查询的结果可以被迭代。这意味着IQueryable不只是查询。正确的术语是它们是表达式树。
现在,这些表达式如何执行以及它们转向什么都取决于所谓的查询提供者(我们可以想到表达式执行器)。
在实体框架世界(即神秘的底层数据源提供程序或查询提供程序)中,IQueryable表达式被转换为本地T-SQL查询。Nhibernate对它们做了类似的事情。例如,您可以按照《LINQ:构建IQueryable Provider链接》中描述的概念编写自己的查询工具,并且您可能希望为您的产品存储提供者服务提供一个自定义查询API。
基本上,IQueryable对象一直在被构造直到我们显式地释放它们并告诉系统把它们重写成SQL或者其他什么然后发送到执行链进行后续处理。
就像延迟执行一样,它是一个LINQ特性,在内存中保留表达式树方案,并仅在需要时将其发送到执行中,无论何时针对序列调用某些api(相同的Count、ToList等)。
The proper usage of both heavily depends on the tasks you're facing for the specific case. For the well-known repository pattern I personally opt for returning IList, that is IEnumerable over Lists (indexers and the like). So it is my advice to use IQueryable only within repositories and IEnumerable anywhere else in the code. Not saying about the testability concerns that IQueryable breaks down and ruins the separation of concerns principle. If you return an expression from within repositories consumers may play with the persistence layer as they would wish.
A little addition to the mess :) (from a discussion in the comments))
None of them are objects in memory since they're not real types per se, they're markers of a type - if you want to go that deep. But it makes sense (and that's why even MSDN put it this way) to think of IEnumerables as in-memory collections whereas IQueryables as expression trees. The point is that the IQueryable interface inherits the IEnumerable interface so that if it represents a query, the results of that query can be enumerated. Enumeration causes the expression tree associated with an IQueryable object to be executed.
So, in fact, you can't really call any IEnumerable member without having the object in the memory. It will get in there if you do, anyways, if it's not empty. IQueryables are just queries, not the data.