LINQ query reuse and deferred execution

北慕城南 提交于 2019-12-31 03:59:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!