我有一个时间表列表,这些时间表都在不同的日子开始和结束。当我搜索一周内的所有时间表时,我想列出该周范围内的所有时间段,即使是那些可能开始或结束但一周内有一天或多天的时间段,请参见图像示例:
为了解决这个问题,我编写了以下代码:
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)
);
这段代码运行良好,所以我现在的问题是是否有办法优化它或者只是让代码看起来更好?