假设我有
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);
}
最佳答案
当您调用 .ToArray
方法时,会对集合执行一次迭代,因此两者应该是等效的。 .Select
是一个投影,.Where
是一个过滤器,都表示为原始数据集上的表达式树。
可以很容易地证明:
public class Foo: IEnumerable<int>
{
public IEnumerator<int> GetEnumerator()
{
yield return 1;
Console.WriteLine("we are at element 1");
yield return 2;
Console.WriteLine("we are at element 2");
yield return 3;
Console.WriteLine("we are at element 3");
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
class Program
{
static void Main()
{
var filtered = new Foo()
.Select(item => item * 10)
.Where(item => item < 20)
.ToList();
}
}
运行时打印如下:
we are at element 1
we are at element 2
we are at element 3
关于c# - Select 后跟 Where 会导致对 IEnumerable 进行两次迭代吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7988146/