LINQ执行流程(作业) [英] LINQ execution flow (homework)

查看:70
本文介绍了LINQ执行流程(作业)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不理解以下代码中的执行顺序.这里满足第一个Where子句的数字是(4、10、3、7),满足第二个Where子句的数字是2和1,之后我们有函数Aggregate减去它们并两者兼而有之.

I don't understand the order of execution in the following code. Here the numbers that satisfied the first Where clause are (4, 10, 3, 7), and the numbers that satisfied the second Where clause are 2 and 1, after that we have function Aggregate that subtract them and make one element from both.

我的问题是,这里的执行流程是什么-(1)对所有元素使用c/3> 0执行Where,然后(2)Where或(1)第一个子句被执行一个元素并将其传递给(2)并从那里进行聚合-当我打印值时,使用两种方法我都无法在纸上获得x的值为28,也无法调试linq语句.感谢您的任何事先帮助.

My question is what is the flow of execution here - (1) Where is executed with c/3 > 0 for all the elements and after that (2) Where or (1) first clause is executed for one element and its passed to (2) and from there to aggregate - when I print the values I cannot get value of x to be 28 on paper with both approaches also I cannot debug linq statement. Thanks for any help in advance.

var ints = new int[] { 2, 4, 1, 10, 3, 7 };

var x = ints
    .Where(c => c / 3 > 0)               <-- (1)
    .Select(s2 => s2 + ints
        .Where(c => c / 3 == 0)          <-- (2)
        .Aggregate((f, s) => f - s))     
    .Sum();

推荐答案

相同的语句可以编写如下:

The same statement can be written as follows:

var ints = new int[] { 2, 4, 1, 10, 3, 7 };

var x = ints
    .Where(c =>
        {
            Console.WriteLine($"1 Where for number: {c}");
            return c / 3 > 0;
        }) //< --(1)
    .Select(s2 => s2 + ints
        .Where(c =>
        {
            Console.WriteLine($"2 Where for number: {c}");
            return c / 3 == 0;
        }) // < --(2)
        .Aggregate((f, s) =>
        {
            Console.WriteLine($"Aggregate: f: {f} s: {s}");
            return f - s;
        }))
    .Sum();

在此,每个速记lambda表达式都可以编写为带有方法主体的完整匿名方法.您只需要使用{ .. }括号即可.在其中可以编写多个语句.如果您查看中,您会看到它期望(以您的情况为例)Func<int, bool>作为输入参数.这意味着您在内部传递了int返回了一个bool.这就是为什么您需要像我一样编写显式return语句的原因:return c / 3 > 0;

In this every shorthand lambda expression can be written as a complete anonymous method with a method body. You just need to use the { .. } parentheses. Inside them you can write multiple statements. If you check the documentation for Where you can see that it expects (in your case) a Func<int, bool> as input parameter. That means that you pass an int inside and return a bool. This is why you need to write the explicit return statement as I did: return c / 3 > 0;

如果您现在在控制台中插入调试输出,您将获得书面证明并深入了解整个代码仓的执行情况.

If you now insert there a debug output to the console you will get a written proof and insight into the execution of the entire code compartment.

结果输出如下:

1 Where for number: 2 1 Where for number: 4 2 Where for number: 2 2 Where for number: 4 2 Where for number: 1 Aggregate: f: 2 s: 1 2 Where for number: 10 2 Where for number: 3 2 Where for number: 7 1 Where for number: 1 1 Where for number: 10 2 Where for number: 2 2 Where for number: 4 2 Where for number: 1 Aggregate: f: 2 s: 1 2 Where for number: 10 2 Where for number: 3 2 Where for number: 7 1 Where for number: 3 2 Where for number: 2 2 Where for number: 4 2 Where for number: 1 Aggregate: f: 2 s: 1 2 Where for number: 10 .... ....

1 Where for number: 2 1 Where for number: 4 2 Where for number: 2 2 Where for number: 4 2 Where for number: 1 Aggregate: f: 2 s: 1 2 Where for number: 10 2 Where for number: 3 2 Where for number: 7 1 Where for number: 1 1 Where for number: 10 2 Where for number: 2 2 Where for number: 4 2 Where for number: 1 Aggregate: f: 2 s: 1 2 Where for number: 10 2 Where for number: 3 2 Where for number: 7 1 Where for number: 3 2 Where for number: 2 2 Where for number: 4 2 Where for number: 1 Aggregate: f: 2 s: 1 2 Where for number: 10 .... ....

这篇关于LINQ执行流程(作业)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆