I was under the impression that I could create a LINQ query and then reuse it while change the parameters involved. But it seems that you cant change the source collection. Can someone give me a good explanation as to why, as I have clearly misunderstood something fundamental.
Here is some example code.
var source = Enumerable.Range(1, 10);
var value = source.Where(x => x > 5);
var first = value.ToArray();
source = Enumerable.Range(11, 20);
var second = value.ToArray();
I was expecting first to be 6,7,8,9,10 and second to be 11 to 20.
Because
value = source.Where(x => x > 5)
eagerly evaluates the value ofsource
, but defers the evaluation of thex => x > 5
part. when you reassign source, the original range is still there, source is just pointing to a different range. In short, the values inside the lambda are evaluated lazily.Example of the deferred execution
Example of accessing source lazily (i would not recommend this type of code, it's an example of how accessing the
source
variable in a lambda creates a 'closure' that can defer the access tosource
When you do:
You are creating a new object. However, the
Where
query still has a reference to the old object.Find a difference ;)