从下面的查询我很难获得失败百分比,因为它始终为零。难道我做错了什么。
from n in calculateOrderStatus
group n by new { n.OrderDate.Date, n.OrderDate.Hour } into h
select new
{
Failed = h.Sum(n => n.Failed),
Success = h.Sum(n => n.Success),
FailurePercentage = (h.Sum(n => n.Failed) / h.Sum(n => n.Success)) * 100
}
答案 0 :(得分:0)
你正在进行整数除法,而不是浮点数:因此,如果失败,则将失败除以成功将始终为零。成功。乘以100先失败,然后除以成功。
即。 FailurePercentage = 100 * h.Sum(n => n.Failed) / h.Sum(n => n.Success)
答案 1 :(得分:0)
尝试将计算的操作数转换为浮点类型:
FailurePercentage = ((double)h.Sum(n => n.Failed) / (double)h.Sum(n => n.Success)) * 100.0
(虽然技术上铸造第一个就足够了)