2

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:

enter image description here

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?

Jennica
  • 175
  • 1
  • 1
  • 6
  • This is a bad title. Please read http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title – Soner Gönül Apr 26 '13 at 12:08
  • 2
    If it works, then your question may better be placed at: http://codereview.stackexchange.com/. Code looks good to me, you used lambda expressions, so the code is more readable, guess that's all you can do. – jAC Apr 26 '13 at 12:08
  • You can use Betweens to shorten your query. See http://stackoverflow.com/a/13470099/2231703 – HikeMike Jul 04 '13 at 11:37

2 Answers2

0
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)
    );

if the start date is less than the weekend and greater that then week start the end doesn't matter. e.g. your appointment starts or ends within the week.
This is the same for the end date

var thisWeek = schedules.Where(x =>
        (x.ScheduleStartDate >= weekStart && x.ScheduleStartDate <= weekEnd) || 
        (x.ScheduleEndDate <= weekEnd && x.ScheduleEndDate >= weekStart)
    );

On the assumption that the end date is after the start date and the week starts before it ends you can now reduce this further

var thisWeek = schedules.Where(x =>
        (x.ScheduleStartDate <= weekEnd && x.ScheduleEndDate >= weekStart)
    );

This now reads that the appointment starts before the week ends and ends after the week starts.

Since you want a minimum of one day overlap

var thisWeek = schedules.Where(x =>
        x.ScheduleStartDate <= weekEnd.AddDays(-1) && x.ScheduleEndDate >= weekStart.AddDays(1)
    );
James
  • 9,774
  • 5
  • 34
  • 58
-1

You could use a function (or even 4 functions) with a good name instead of a long boolean expression. The function would of course do nothing new but you would have a better readability of the code in later days

BenMorel
  • 34,448
  • 50
  • 182
  • 322
WhileTrueSleep
  • 1,524
  • 1
  • 19
  • 32