我想圆整一个平均Linq函数的结果,有可能吗?圆平均Linq函数C#
public IEnumerable<GetLocationsAverageDTO> GetLocationsAverageByCompanyId(int id)
{
try
{
var baseQuery = _dbContext.Answers.AsNoTracking()
.Where(p => p.Location.Company.Id == id
&& p.Question.Type != QuestionType.Text)
.GroupBy(g => new { Location = g.Location.Name })
.Select(x =>
new GetLocationsAverageDTO
{
LocationName = x.Key.Location,
Average = x.Average(f => f.Numeric)
})
.OrderBy(x => x.LocationName);
var dto = baseQuery.ToList();
return dto;
}
catch (Exception error)
{
_logger.Error("[Error in SurveyService.GetLocationsAverageByCompanyId - id: " + id + " - Error: " + error + "]");
return null;
}
}
喜欢的东西
Average = x.Average(f => f.Numeric, 2) // result: 45,21
这不会有很多工作要做使用LINQ,更多的是与舍入数到所需的精度,无论它来自何方。 –
你不能使用'Math.Round(x.Average(f => f.Numeric),2);'? – Habib
任何请求将内存中的双/十进制“舍入”,而不是将其格式化为文本时,通常是错误的。你确定在这个确切点上需要它吗? –