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:很高兴能击败你,找到一个改变的答案!:)