简体   繁体   English

LINQ MAX懒惰评估

[英]linq max lazy evaluation

I'm having a bit of trouble understanding how lazy evaluation relates to the linq Max operator. 我在理解惰性评估与linq Max运算符之间的关系时遇到了一些麻烦。

This is my code: 这是我的代码:

        var someList = new List<someType>
        {
            new someType { decimalValue = 32 }
        };

line1: var valA = someList.Any() ? someList.Max(s => s.decimalValue) : 0.0M;

line2: var valB = someList.Any() ? someList.Max(s => s.decimalValue) : 0.0M;

line3: var valC = valB;

When stepping through the code I see the following behaviour: 单步执行代码时,我看到以下行为:

After line 1 has executed, valA = 0, valB = 0, valC = 0 在执行第1行之后, valA = 0, valB = 0, valC = 0

After line 2 has executed, valA = 32, valB = 0, valC = 0 第2行执行后, valA = 32, valB = 0, valC = 0

After line 3 has executed, valA = 32, valB = 32, valC = 32 在执行第3行后, valA = 32, valB = 32, valC = 32

Why does valA only show a value after valB is assigned, and why does valB only show a value once valC is assigned? 为什么valA只显示后的值valB分配,以及为什么valB只显示一个值,一旦valC的分配情况?

Below is a screenshot of my debugger. 下面是我的调试器的屏幕截图。 I guess there must be something non-standard about my IDE. 我想我的IDE一定有一些非标准的东西。

IDE映像
(source: toastermagic.co.uk ) (来源: toastermagic.co.uk

The Max operator executes immediately, it is not deferred at all. Max运算符会立即执行,根本不会延迟。 What you're seeing is just the normal behavior of the debugger. 您所看到的只是调试器的正常行为。 When it has a line highlighted it means, "I'm about to run this line" rather than, "I just ran this line". 突出显示一行时,它表示“我将要运行此行”,而不是“我只是运行了此行”。 You'll always need to be on the 'next' line to see the results of a given line of code. 您总是需要在“下一行”上查看给定代码行的结果。

It seems that you have accounted for this. 看来您已经考虑了这一点。 The fact that you are see all changes (this shouldn't be just LINQ specific) a line late probably means your source code and the executable are a bit out of sync. 您会在一行之后看到所有更改(这不仅是LINQ特定的),这可能意味着您的源代码和可执行文件有些不同步。 You should do a clean and re-bulid of the solution and then everything should be fine. 您应该对解决方案进行重新整理,然后一切都应该没问题。 I'm guessing that you made some small modification to the source code that didn't get picked up properly in a build and so the line numbers in the executable vs. the source code are each off by one. 我猜想您对源代码进行了一些小的修改,而这些修改没有在构建中正确获取,因此可执行文件与源代码中的行号各相距一个。

If something is really, really wrong, you can try moving the code to a new project entirely (if your code base is small enough) in an attempt to get rid of whatever corrupted or broken file/setting is causing this problem. 如果确实存在某些问题,则可以尝试将代码完全移到新项目中(如果您的代码库足够小),以消除引起该问题的任何损坏或损坏的文件/设置。

Cannot Replicate: 无法复制:

    static void Main(string[] args)
    {
        var data = (from n in Enumerable.Range(0, 10)
                    select new { value = (decimal)n })
                   .ToList();

        var valA = data.Any() ? data.Max(s => s.value) : 0.0M;
        Console.WriteLine("A = {0}, B = {1}, C = {2}", valA, 0, 0);

        var valB = data.Any() ? data.Max(s => s.value) : 0.0M;
        Console.WriteLine("A = {0}, B = {1}, C = {2}", valA, valB, 0);

        var valC = valB;
        Console.WriteLine("A = {0}, B = {1}, C = {2}", valA, valB, valC);

        Console.ReadLine();
    }

Works as expected. 可以正常工作。

Looks to be a misread of the debugger. 看起来是调试器的误读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM