I recently discovered that there is a big difference between doing some operations, such as Select
and Where
, in Queryable
and enumerated objects, such as Enumerable
and List
. This and this question.
Knowing this, I was curious to know if there are differences between iterating values of a List
and iterating values straight from a Select
.
I saw in the debug that when I only do Select
I'm iterating over {System.Linq.Enumerable.WhereSelectListIterator<string, string>}
.
When I do Where
or Select
in a list does the operation run the instant the method is called or only runs when I access the items or use ToList()
?
Also, is there any performance difference between the two cases?
Is it easier to iterate over a List
or is it more costly to transform to List
to later iterate?
Difference of this:
var lista = new List<string>() { ... };
var listaFiltrada = lista.Select(x => x).ToList();
foreach (var item in listaFiltrada) { ... }
For this:
var lista = new List<string>() { ... };
var selectFiltrado = lista.Select(x => x);
foreach (var item in selectFiltrado) { ... }