I have code that basically assigns a variable in one case, else a different in another. Is there a neater way that is more efficient?
I.e., not taking up so many lines. Or is this the best way?
if (ViewBag.Date != RoomBooking.StartDateTime.Date || ViewBag.DayPlannerStartTime * 12 > (Int32)RoomBooking.StartDateTime.TimeOfDay.TotalMinutes / 5)
{
StartBlock = ViewBag.DayPlannerStartTime * 12;
}
else
{
StartBlock = ((Int32)RoomBooking.StartDateTime.TimeOfDay.TotalMinutes / 5);
}
Maybe this (it’s arguably neater, but definitely has the same efficiency):
EDIT: You can slightly optimize the condition as well. I suspect that your
DayPlannerStartTimeis expressed in seconds, and if I’m right you can rewrite the comparison the following way (I just divided both operands of the>operator by 12, andTotalMinutesdivided by 5*12 becameTotalHours):