LINQ和递延评价 [英] Linq and deferred evaluation
问题描述
正如我已经学会了LINQ,
As I have learned LINQ,
的当你使用LINQ通过使用LINQ的扩展定义枚举集合,方法或通过使用查询运算符,应用程序
实际上不构建在所执行的LINQ
扩展方法时的收集; B您遍历它集合枚举只有当$ B $。这意味着原始
集合中的数据可以在执行LINQ查询和检索
该查询标识数据之间变化;你总是会获取最
向上的最新数据。的
微软的Visual C#2013步步由约翰·夏普撰写
我已经写了下面的代码:
I have written the following code:
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 };
IEnumerable<int> res = numbers.FindAll(a => a > 0).Select(b => b).ToList();
numbers.Add(99);
foreach (int item in res)
Console.Write(item + ", ");
以上代码的结果如下:
The result of the above code is as follows:
1, 2, 3, 4, 5,
我不明白为什么会这样。我知道Func键,动作和谓语,但我无法弄清楚是怎么回事。谢谢可能的答案。 ;)
I do not understand why it is going like this. I know about Func, Action and Predicate but I can not figure out what is going on here. Thanks for probable answers. ;)
推荐答案
除了在最后的了ToList()
,这是创建一个新的集合,你有另外一个问题。
Apart from the ToList()
at the end, which is creating a new collection, you have another issue.
的问题是,你不使用LINQ的。
The problem is that you are not using LINQ at all.
的FindAll
不是LINQ的扩展方法。
FindAll
is not a LINQ extension method.
您应该使用在哪里
:
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 };
IEnumerable<int> res = numbers.Where(a => a > 0);
numbers.Add(99);
foreach (int item in res)
Console.Write(item + ", ");
这篇关于LINQ和递延评价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!