相关疑难解决方法(0)

延迟LINQ查询执行实际上如何工作?

最近我遇到了这样的问题: What numbers will be printed considering the following code:

class Program
{
    static void Main(string[] args)
    {
        int[] numbers = { 1, 3, 5, 7, 9 };
        int threshold = 6;
        var query = from value in numbers where value >= threshold select value;

        threshold = 3;
        var result = query.ToList();

        result.ForEach(Console.WriteLine);
        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

回答: 3, 5, 7, 9

这让我很惊讶.我认为该threshold值将在查询构造中放入堆栈,稍后在执行时,该数字将被拉回并在条件中使用..这种情况没有发生.

另一种情况(在执行之前numbers设置null):

    static void Main(string[] args)
    {
        int[] numbers = { 1, 3, …
Run Code Online (Sandbox Code Playgroud)

c# linq

26
推荐指数
1
解决办法
1865
查看次数

标签 统计

c# ×1

linq ×1