LINQ查询重用和延迟执行 [英] LINQ query reuse and deferred execution
本文介绍了LINQ查询重用和延迟执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是,我可以创造一个LINQ查询,然后重新使用它而改变相关参数的印象。但似乎你不能改变的源集合。有人可以给我一个很好的解释,为什么,因为我已经清楚地误解了一些基本的东西。
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.
下面是一些示例代码。
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();
我期待第一次是6,7,8,9,10和第二把要了11 20
I was expecting first to be 6,7,8,9,10 and second to be 11 to 20.
推荐答案
当你这样做:
source = Enumerable.Range(11, 20);
您正在创建一个新的对象。但是,其中,
查询仍然有旧的对象的引用。
You are creating a new object. However, the Where
query still has a reference to the old object.
这篇关于LINQ查询重用和延迟执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文