我正在尝试编写一个linq语句,它将执行以下操作。我需要在一个linq语句中计算平均值。什么是最好的方法呢
var onlyLayer0 = trigger2Output.NpvResults.CommIns.Where(x => x.layerId == 0);
var numberOfRecords = onlyLayer0.Count();
var totalPremiumPaid = onlyLayer0.Sum(x => x.premPaid);
var avgpremiumPaid = totalPremiumPaid / numberOfRecords;
我目前的linq声明
trigger2Output.NpvResults.CommIns.Where(x => x.layerId == 0)
.Sum(x => x.premPaid)
答案 0 :(得分:0)
尝试平均延期:
trigger2Output.NpvResults.CommIns.Where(x => x.layerId == 0).Average(x => x.premPaid)
这里是MSDN文档。
答案 1 :(得分:0)
试试这个:
trigger2Output.NpvResults.CommIns.Where(x => x.layerId == 0).Average(r => r.premPaid);