简体   繁体   English

Lambda中的几个语句

[英]several statements in lambda

I have the following statement 我有以下声明

var evarage = productionreportentry
              .Sum(productionReportEntry => productionReportEntry.Cycletime);

I would like to add some logging inside the Sum lambda. 我想在Sum lambda中添加一些日志记录。 Is this possible? 这可能吗?

Yep, do something like: 是的,执行以下操作:

var evarage = productionreportentry.Sum(productionReportEntry => 
{ 
   Trace.Writeline(productionReportEntry.Cycletime);
   return productionReportEntry.Cycletime;
});

Basically you add the curly brackets, and you need to explicitly return the value that the lambda is operating on, in this case the Cycletime which is used as part of the sum. 基本上,您添加了大括号,并且需要显式返回lambda所操作的值,在这种情况下,将Cycletime用作总和的一部分。

I am not sure what you really want but you can have multiple statements like this 我不确定您真正想要的是什么,但是可以有多个这样的语句

        var evarage = productionreportentry.Sum(productionReportEntry =>
            {
                CreateLog();
                return productionReportEntry.Cycletime;
            });

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

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