问题
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.
回答1:
When you do:
source = Enumerable.Range(11, 20);
You are creating a new object. However, the Where
query still has a reference to the old object.
回答2:
source = Enumerable.Range(11, 20);
var second = value.ToArray();
second = Enumerable.Range(11, 20);
var second = value.ToArray();
Find a difference ;)
回答3:
Because value = source.Where(x => x > 5)
eagerly evaluates the value of source
, but defers the evaluation of the x => 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
source = Enumerable.Range(1,10).ToArray();
value = source.Where(x => x > 5);
var first = value.ToArray(); // 6,7,8,9,10
source.[0] = 100;
var second = value.ToArray(); // 100,6,7,8,9,10
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 to source
source = Enumerable.Range(1,10);
value = Enumerable.Range(1).SelectMany(n => source.Where(x => x > 5));
var first = value.ToArray();
source = Enumerable.Range(11,20);
var second = value.ToArray();
来源:https://stackoverflow.com/questions/11565469/linq-query-reuse-and-deferred-execution