I have a list of schedules that all start and end on different days. When I search for all schedules in a week I want to list all time periods that are within range of that week, even those that may start or end outside but has one or more days within the week, see image example:
To sort this out i've made the following code:
var weekStart = DateTime.Now().Date;
var weekEnd = weekStart.AddDays(6);
var thisWeek = schedules.Where(x =>
(x.ScheduleStartDate >= weekStart && x.ScheduleStartDate <= weekEnd && x.ScheduleEndDate <= weekEnd && x.ScheduleEndDate >= weekStart) ||
(x.ScheduleStartDate <= weekStart && x.ScheduleEndDate <= weekEnd && x.ScheduleEndDate >= weekStart) ||
(x.ScheduleStartDate >= weekStart && x.ScheduleStartDate <= weekEnd && x.ScheduleEndDate >= weekEnd) ||
(x.ScheduleStartDate <= weekStart && x.ScheduleEndDate >= weekEnd)
);
This code works fine so my question now is if there is a way to optimize it or just make the code look nicer?