Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# lambda中的几个语句_C#_.net_Lambda - Fatal编程技术网

C# lambda中的几个语句

C# lambda中的几个语句,c#,.net,lambda,C#,.net,Lambda,我有以下声明 var evarage = productionreportentry .Sum(productionReportEntry => productionReportEntry.Cycletime); 我想在Sumlambda中添加一些日志记录。这可能吗?是的,请执行以下操作: var evarage = productionreportentry.Sum(productionReportEntry => { Trace.Write

我有以下声明

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

我想在
Sum
lambda中添加一些日志记录。这可能吗?

是的,请执行以下操作:

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

基本上,您需要添加花括号,并且需要显式返回lambda操作的值,在本例中,循环时间用作总和的一部分。

是的,请执行以下操作:

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

基本上,添加花括号,需要显式返回lambda操作的值,在本例中为循环时间,循环时间用作求和的一部分。

我不确定您真正想要的是什么,但您可以有多个这样的语句

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

我不确定你到底想要什么,但你可以有多个这样的陈述

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

对不起,我不明白你的问题。你能澄清一下吗?我不明白你说的兰姆达车里面是什么意思。你想记录什么?很抱歉,我不明白你的问题。你能澄清一下吗?我不明白你说的兰姆达车里面是什么意思。您要记录的是什么?请注意,这只适用于LINQ to对象-任何需要表达式树而不是委托的操作都会失败,因为语句lambdas无法转换为表达式树。@JonSkeet:我从未使用过表达式树(甚至100%不确定它们是什么),因为我只在.NET 2.0中使用。但是谢谢你的评论Jon:)@JonSkeet:很高兴能击败你,找到一个改变的答案!:)请注意,这仅适用于LINQ to对象-任何需要表达式树而不是委托的操作都将失败,因为语句lambdas无法转换为表达式树。@JonSkeet:我从未使用过表达式树(甚至100%不确定它们是什么),因为我只在.NET 2.0中使用。但是谢谢你的评论Jon:)@JonSkeet:很高兴能击败你,找到一个改变的答案!:)