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);
}
There is a single iteration over the collection performed when you call the
.ToArraymethod so both should be equivalent..Selectis a projection and.Whereis a filter, both expressed as expression trees on the original dataset.Could be easily proven:
when run prints the following: