问题描述
-
如果
IQueryable
接口在服务器中执行查询表达式,而不是像IEnumerable
那样获取所有记录,为什么IQueryable
不能用IEnumerable
代替,因为它可以更快,更高效? /p> -
DBSet<T>
具有Where
的两种风格(IQueryable
和IEnumerable
).是否可以调用IEnumerable
版本,因为默认情况下会调用IQueryable
而不调用ToList()
?
IQueryable
和IEnumerable
代表两个不同的事物.将IQueryable
视为问题",它本身没有任何结果. IEnumerable
是一个答案",它仅连接了数据,但是您无法确定是什么原因产生了该数据.
并不是说IQueryable
可以说更快",它只是允许您将过滤和预测放入您向SQL Server提出的问题"中,并使其仅返回所需的答案. (通过调用.ToList()
或类似形式以IEnumerable
的形式).
如果仅使用IEnumerable
,则您可以问的唯一问题是给我您所知道的一切",然后根据答案进行过滤和投影.这就是为什么IQueryable
被认为更快的原因,因为您能够向服务器提出更具体的问题,因此需要处理的数据更少.
IQueryable
不能在所有地方替换IEnumerable
的原因是因为您要提出问题的事物必须能够理解您所提出的问题.要解析所有可能要过滤或投影在所有实现上的可能事情,需要花费大量的工作,因此大多数实现将自己仅限于他们知道自己需要能够回答的普通事物.例如,在Entity Framework中,当您问一个不了解如何处理的问题时,当您尝试获取IEnumerable
时,将得到一条类似于不支持指定方法" 的错误. (答案).
类DBSet<T>
没有其中的方法完全可以.这两个Where
函数来自两个扩展方法, Enumerable.Where
和 Queryable.Where
.通过在调用扩展方法之前将对象强制转换为IEnumerable<T>
,可以强制其使用Enumerable重载.但是请记住,Queryable.Where
过滤问题,Enumerable.Where
仅过滤结果.
从服务器请求结果然后将其丢弃是很浪费的,所以我不建议您这样做.
If the
IQueryable
interface performs the query expression in the server rather than fetching all records likeIEnumerable
, why isIQueryable
not replaced byIEnumerable
where it can be faster and more efficient?DBSet<T>
has two flavors ofWhere
(IQueryable
andIEnumerable
). Is there a way to call theIEnumerable
version because theIQueryable
is called by default, without callingToList()
?
IQueryable
and IEnumerable
represent two different things. Think of a IQueryable
as a "question", it does not have any results itself. A IEnumerable
is an "answer", it only has data connected to it but you can't tell what generated that data.
It is not that a IQueryable
is "faster" per say, it just allows you to put your filtering and projections in to the "question" you ask to the SQL server and let it return only the answers it needs to (In the form of a IEnumerable
by calling .ToList()
or similar).
If you only use a IEnumerable
the only question you can ask is "Give me everything you know" then on the answer it gives you you perform your filtering and projections. That is why IQueryable
is considered faster, because there is a lot less data that needs to be processed because you where able to ask a more specific question to the server.
The reason IQueryable
has not replaced IEnumerable
everywhere is because the thing you are asking a question has to be able to understand the question you are asking it. It takes a lot of work to be able to parse every possible thing you could ask it to filter or project on so most implementations limit themselves to only common things they know they need to be able to answer. For example in Entity Framework when you ask a question it does not understand how to handle you will get a error that says something similar to "Specified method is not supported" when you try to get a IEnumerable
(an answer) from the IQueryable
.
The class DBSet<T>
has no Where method on it at all. The two Where
functions come from two extension methods, Enumerable.Where
and Queryable.Where
. You can force it to use the Enumerable overload by casting the object to a IEnumerable<T>
before you call the extension method. However do remember, Queryable.Where
filters the question, Enumerable.Where
only filters the result.
It is wasteful to ask for results from the server to then just throw them away, so I would not recommend doing this.
这篇关于IQueryable与IEnumerable:IQueryable是否总是越来越好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!