Collection of schedules I hava a collection of schedules - EventSchedules. I want to select from this collection today, tomorrow and after tomorrow schedules. The day start from 06:00 and end 6:00 of next day.  

Examples:


For today the query return events start in range: 30 July 2012 06:00 - 31 July 2012 06:00
For tomorrow the range is: 31 July 2012 06:00 - 01 August 2012 06:00


Any advice for refactoring this code?   

private void FillSchedulesByFilter()
{
    var filterTimeStart = DateTime.Today.AddHours(6);
    var filterTimeEnd = filterTimeStart.AddDays(1);

    TodayGroupedSchedules = EventScheduleList.Where(s =&gt; s.RecurrenceStart &gt;= filterTimeStart &amp;&amp;
                              s.RecurrenceStart &lt; filterTimeEnd).
                              GroupBy(p =&gt; p.BasePlace);

    filterTimeStart = filterTimeStart.AddDays(1);
    filterTimeEnd = filterTimeEnd.AddDays(1);

    TomorrowGroupedSchedules = EventScheduleList.Where(s =&gt; s.RecurrenceStart &gt;= filterTimeStart &amp;&amp;
                               s.RecurrenceStart &lt; filterTimeEnd).
                               GroupBy(p =&gt; p.BasePlace);

    filterTimeStart = filterTimeStart.AddDays(1);
    filterTimeEnd = filterTimeEnd.AddDays(1);

    AfterTomorrowGroupedSchedules = EventScheduleList.Where(s =&gt; s.RecurrenceStart &gt;= filterTimeStart &amp;&amp;
                               s.RecurrenceStart &lt; filterTimeEnd).
                               GroupBy(p =&gt; p.BasePlace);

}
I can see two issues with your code:


It doesn't work. Because the local variables filterTimeStart and filterTimeEnd are put in the closure, and the Where() lambdas are executed only when you iterate the resulting enumerables, all of your schedules will be the same as AfterTomorrowGroupedSchedules, because the last value assigned to the locals will be used. You can fix this by using ToArray().
It's not DRY. The whole LINQ expression is always exactly the same and the interval is always one day. You should extract that into another method.


The modified code:

private IEnumerable&lt;IGrouping&lt;string, EventSchedule&gt;&gt; GroupedSchedulesForADay(
    DateTime filterTimeStart)
{
    return EventScheduleList.Where(
        s =&gt; s.RecurrenceStart &gt;= filterTimeStart &amp;&amp;
            s.RecurrenceStart &lt; filterTimeStart.AddDays(1))
        .GroupBy(p =&gt; p.BasePlace)
        .ToArray();
}

private void FillSchedulesByFilter()
{
    var filterTimeStart = DateTime.Today.AddHours(6);

    TodayGroupedSchedules = GroupedSchedulesForADay(filterTimeStart);

    filterTimeStart = filterTimeStart.AddDays(1);

    TomorrowGroupedSchedules = GroupedSchedulesForADay(filterTimeStart);

    filterTimeStart = filterTimeStart.AddDays(1);

    AfterTomorrowGroupedSchedules = GroupedSchedulesForADay(filterTimeStart);
}


Technically, the ToArray() is not necessary anymore, because each invocation of the GroupedSchedulesForADay() method has its own local variable, but I think it's still a good idea.

Also, I was worried that AddHours(6) wouldn't work correctly in the days where daylight saving time changes, but it seems it does work.

If the collection was big, it could make sense to iterate it just once, instead of three times, and build the three collections manually item by item, but it seems that's not necessary here.