1
I recently discovered that there’s a big difference between doing some operations, like Select
and Where
, in Queryable
and listed objects such as Enumerable
and List
. In this and in this question.
Knowing this, I was curious to know if there are differences between iterating values of a List
and iterate values directly from a Select
.
I saw in the debug that when I just do Select
I’m iterating on a {System.Linq.Enumerable.WhereSelectListIterator<string, string>}
.
When I do Where
or Select
in a list the operation is executed at the time the method is called or is only executed when I access the items or use ToList()
?
In addition, there is some difference in performance between the two cases?
It’s easier to iterate on a List
or it is more costly to transform into List
and then iterate?
Other than that:
var lista = new List<string>() { ... };
var listaFiltrada = lista.Select(x => x).ToList();
foreach (var item in listaFiltrada) { ... }
For that reason:
var lista = new List<string>() { ... };
var selectFiltrado = lista.Select(x => x);
foreach (var item in selectFiltrado) { ... }