LINQ query reuse and deferred execution

2019-03-03 04:56发布

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.

3条回答
贪生不怕死
2楼-- · 2019-03-03 05:40

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();
查看更多
姐就是有狂的资本
3楼-- · 2019-03-03 05:44

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.

查看更多
男人必须洒脱
4楼-- · 2019-03-03 06:03
source = Enumerable.Range(11, 20);
    var second = value.ToArray();

second = Enumerable.Range(11, 20);
    var second = value.ToArray();

Find a difference ;)

查看更多
登录 后发表回答