Split a date range into several ranges?

I have a start date and an end date (in sql server datetime format). I want to split this into several ranges ie several pairs of start and end date values. NOTE - I have .NET 3.5 and Visual studio 2008.

Eg. S = 2005. E = 2010, Chunk size = 1 year. Paris generated = 2005-06, 06-07, 07-08, 08-2010

The chunks can be of any number of days/months. I put the code in an SO post, after my main method and I get some errors. Post - Split date range into date range chunks

Code -

public static IEnumerable<Tuple<DateTime, DateTime>> SplitDateRange(DateTime start, DateTime end, int dayChunkSize)
{
    DateTime chunkEnd;
    while ((chunkEnd = start.AddDays(dayChunkSize)) < end)
    {
        yield return Tuple.Create(start, chunkEnd);
        start = chunkEnd;
    }
    yield return Tuple.Create(start, end);
}

I get two errors:

The body of 'CodeHere.csproj.ScriptMain.SplitDateRange(System.DateTime, System.DateTime, int)' cannot be an iterator block because 'IEnumerable

And:

The name 'Tuple' does not exist in the current context

Jon Skeet
people
quotationmark

You're trying to use System.Tuple<T1, T2>, which was only introduced in .NET 4. You're using .NET 3.5, so that structure isn't available. I suggest you create your own DateRange type which encapsulates a start and end DateTime, then return an IEnumerable<DateRange> instead.

Either that, or upgrade to .NET 4 or 4.5...

people

See more on this question at Stackoverflow