IQueryable 与 IEnumerable:IQueryable 总是更好更快吗?
IQueryable vs IEnumerable: is IQueryable always better and faster?
如果IQueryable
接口在服务器中执行查询表达式而不是像IEnumerable
那样获取所有记录,为什么IQueryable
没有被[=11取代=] 哪里可以更快更高效?
DBSet<T>
有两种形式的 Where
(IQueryable
和 IEnumerable
)。有没有办法调用 IEnumerable
版本,因为默认调用 IQueryable
,而不调用 ToList()
?
- If the
IQueryable
perform the query Expression in the server rather
than fetching all records like IEnumerable
, why IQueryable
not
replaced by IEnumerable
where it can be faster and more efficient?
IQueryable
和IEnumerable
代表两个不同的东西。将 IQueryable
视为 "question",它本身没有任何结果。 IEnumerable
是一个 "answer",它只有数据与之相关,但您无法分辨是什么生成了该数据。
并不是说 IQueryable
是 "faster",它只是允许您将您的过滤和投影放入 "question" 您要求 SQL 服务器并让它 return 只得到它需要的答案(以 IEnumerable
的形式调用 .ToList()
或类似的)。
如果您只使用 IEnumerable
,您唯一可以问的问题是 "Give me everything you know",然后根据它给您的答案执行过滤和预测。这就是为什么 IQueryable
被认为更快的原因,因为需要处理的数据要少得多,因为您可以向服务器提出更具体的问题。
之所以 IQueryable
没有在任何地方取代 IEnumerable
是因为你提出问题的东西必须能够理解你提出的问题。需要做大量的工作才能解析您可能要求它过滤或投射到大多数实现上的所有可能的东西 limit themselves to only common things they know they need to be able to answer。例如在 Entity Framework 中,当你问一个问题时它不明白如何处理你会得到一个错误,当你试图得到来自 IQueryable
.
的 IEnumerable
(答案)
DBSet<T>
has two flavors of Where
(IQueryable
and IEnumerable
).
is there a way to call the IEnumerable
version because the
IQueryable
is called by default, without calling ToList()
?
classDBSet<T>
没有Where method on it at all. The two Where
functions come from two extension methods, Enumerable.Where
and Queryable.Where
。在调用扩展方法之前,您可以通过将对象强制转换为 IEnumerable<T>
来强制它使用 Enumerable 重载。但是请记住,Queryable.Where
过滤问题,Enumerable.Where
只过滤结果。
向服务器请求结果然后将其丢弃是一种浪费,所以我不建议这样做。
如果
IQueryable
接口在服务器中执行查询表达式而不是像IEnumerable
那样获取所有记录,为什么IQueryable
没有被[=11取代=] 哪里可以更快更高效?DBSet<T>
有两种形式的Where
(IQueryable
和IEnumerable
)。有没有办法调用IEnumerable
版本,因为默认调用IQueryable
,而不调用ToList()
?
- If the
IQueryable
perform the query Expression in the server rather than fetching all records likeIEnumerable
, whyIQueryable
not replaced byIEnumerable
where it can be faster and more efficient?
IQueryable
和IEnumerable
代表两个不同的东西。将 IQueryable
视为 "question",它本身没有任何结果。 IEnumerable
是一个 "answer",它只有数据与之相关,但您无法分辨是什么生成了该数据。
并不是说 IQueryable
是 "faster",它只是允许您将您的过滤和投影放入 "question" 您要求 SQL 服务器并让它 return 只得到它需要的答案(以 IEnumerable
的形式调用 .ToList()
或类似的)。
如果您只使用 IEnumerable
,您唯一可以问的问题是 "Give me everything you know",然后根据它给您的答案执行过滤和预测。这就是为什么 IQueryable
被认为更快的原因,因为需要处理的数据要少得多,因为您可以向服务器提出更具体的问题。
之所以 IQueryable
没有在任何地方取代 IEnumerable
是因为你提出问题的东西必须能够理解你提出的问题。需要做大量的工作才能解析您可能要求它过滤或投射到大多数实现上的所有可能的东西 limit themselves to only common things they know they need to be able to answer。例如在 Entity Framework 中,当你问一个问题时它不明白如何处理你会得到一个错误,当你试图得到来自 IQueryable
.
IEnumerable
(答案)
DBSet<T>
has two flavors ofWhere
(IQueryable
andIEnumerable
). is there a way to call theIEnumerable
version because theIQueryable
is called by default, without callingToList()
?
classDBSet<T>
没有Where method on it at all. The two Where
functions come from two extension methods, Enumerable.Where
and Queryable.Where
。在调用扩展方法之前,您可以通过将对象强制转换为 IEnumerable<T>
来强制它使用 Enumerable 重载。但是请记住,Queryable.Where
过滤问题,Enumerable.Where
只过滤结果。
向服务器请求结果然后将其丢弃是一种浪费,所以我不建议这样做。