我有以下代码,并且它除了零除外。我如何纠正并改进代码?
List<RelacionEjecucionPendientes> iniciativasEjecucionPendienteses = actividades
.GroupBy(cl => cl.iniciativaName)
.Select(cl => new RelacionEjecucionPendientes
{
Nombre = cl.Key,
ATiempoEjecucionCantidad = cl.Count(c => c.estado != "No Iniciada" && (Convert.ToDateTime(c.fechaVencimiento).Day - actualTime.Day) >= 0),
ATiempoEjecucionPorcentaje = String.Format("{0:0}",
(cl.Count(c => c.estado != "No Iniciada" && (Convert.ToDateTime(c.fechaVencimiento).Day - actualTime.Day) >= 0) /
cl.Count(c => c.estado != "No Iniciada") != 0 ? cl.Count(c => c.estado != "No Iniciada") : 1))
}).ToList();
答案 0 :(得分:6)
Add parentheses to group your conditional operator. Division comes before the conditional (? :
) operator, so it is trying to divide by 0 before the operator:
ATiempoEjecucionPorcentaje = String.Format("{0:0}",
(cl.Count(c => c.estado != "No Iniciada" && (Convert.ToDateTime(c.fechaVencimiento).Day - actualTime.Day) >= 0) /
(cl.Count(c => c.estado != "No Iniciada") != 0 ? cl.Count(c => c.estado != "No Iniciada") : 1)))