Select 后跟Where 是否会导致对IEnumerable 进行两次迭代?
假设我有
IEnumerable<int> list = new int[] { 1, 2, 3 };
List<int> filtered = list.Select(item => item * 10).Where(item => item < 20).ToList();
问题是有两次迭代还是只有一次。
换句话说,它的性能相当于:
IEnumerable<int> list = new int[] { 1, 2, 3 };
List<int> filtered = new List<int>();
foreach(int item in list) {
int newItem = item * 10;
if(newItem < 20)
filtered.Add(newItem);
}
Let say I have
IEnumerable<int> list = new int[] { 1, 2, 3 };
List<int> filtered = list.Select(item => item * 10).Where(item => item < 20).ToList();
The question is are there two iterations or just one.
In other words, is that equivalent in performance to:
IEnumerable<int> list = new int[] { 1, 2, 3 };
List<int> filtered = new List<int>();
foreach(int item in list) {
int newItem = item * 10;
if(newItem < 20)
filtered.Add(newItem);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您调用
.ToArray
方法时,会对集合执行一次迭代,因此两者应该是等效的。.Select
是投影,.Where
是过滤器,两者都表示为原始数据集上的表达式树。可以很容易地证明:
运行时打印以下内容:
There is a single iteration over the collection performed when you call the
.ToArray
method so both should be equivalent..Select
is a projection and.Where
is a filter, both expressed as expression trees on the original dataset.Could be easily proven:
when run prints the following:
在 Linq to Objects 中,WHERE 和 SELECT 不会迭代可枚举值。当调用代码对查询或 ToList 或 ToArray() 等执行 foreach 时,调用代码会枚举它
。在 Linq to SQL 中,没有任何迭代。当您执行 ToList 或 ToArray() 时,查询由数据库执行。根据查询的类型,数据库可以查找索引或进行表扫描。
In Linq to Objects WHERE and SELECT do not iterate over the enumerable. The calling code enumerates it when it does a foreach on the query or ToList or ToArray(), etc.
In Linq to SQL there is no iteration what so ever. When you do ToList or ToArray() the query is executed by database. Depending on the type of query db could look up indexes or do a table scan.