I have the following code and it thow an divide by zero exception. How I can correct that and improve the code?
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();
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)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.