AsEnumerable()是否缓存所有结果(LINQ) [英] Does AsEnumerable() cache all result (LINQ)

查看:93
本文介绍了AsEnumerable()是否缓存所有结果(LINQ)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在序列上调用查询运算符时,将调用特定于序列的运算符.
我的意思是,如果我在IEnumerable<>上调用Where<>()运算符,将在Enumerable类中定义将被调用的运算符,如果在IQueryable<>上调用它,则将在Queryable类中定义的那个运算符被调用.

When we call a query operator on a sequence, a sequence-specific operator gets called.
I mean if i call Where<>() operator on IEnumerable<>, the operator that will be called will be defined in Enumerable class and if it's called on IQueryable<>, the one defined in Queryable class will be called.

考虑在Enumerable类中定义的运算符Reverse.
如果要在Iqueryable<>上调用它,则必须使用AsEnumerable<>()运算符首先将其转换为IEnumerable<>.

Consider the operator Reverse, defined in the Enumerable class.
If i want to call it on Iqueryable<> then I must use the AsEnumerable<>() operator to first convert it into IEnumerable<>.

db.Countries.OrderBy(cntry=>cntry.CountryName).AsEnumerable().Reverse()

但是Reverse运算符必须同时拥有所有记录,以便它可以将它们反转.
在上面的代码中,是否所有记录都首先加载到内存中,然后Reverse()运算符将其反转?

But the Reverse operator got to have all records at the same time so that it can reverse them.
In the above code do all the records get loaded in memory first and then the Reverse() operator is reversing it ?

推荐答案

IQueryable<T>扩展了IEnumerable<T>,因此可枚举.使用List<T>和简单的iterator-generator进行反向操作.

IQueryable<T> extends IEnumerable<T>, so the AsEnumerable is redundant. However, if you use it, AsEnumerable is not going to greedily load the collection (that would be a very annoying "feature"). Clearly, Reverse will need the whole thing. Mono implements Enumerable.Reverse using a List<T> and a simple iterator-generator that yields in reverse.

我相信AsEnumerable的唯一真正目的是强制使用IEnumerable显式接口实现.

I believe the only real purpose of AsEnumerable is to force the use of IEnumerable explicit interface implementations.

这篇关于AsEnumerable()是否缓存所有结果(LINQ)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆