Exploding a range of dates with LINQ

Posted by Robert Gowland on Stack Overflow See other posts from Stack Overflow or by Robert Gowland
Published on 2010-04-16T17:48:12Z Indexed on 2010/04/16 17:53 UTC
Read the original article Hit count: 175

Filed under:
|

If I have a pair of dates, and I want to generate a list of all the dates between them (inclusive), I can do something like:

System.DateTime s = new System.DateTime(2010, 06, 05);
System.DateTime e = new System.DateTime(2010, 06, 09);
var list = Enumerable.Range(0, (e - s).Days)
    .Select(value => s.AddDays(value));

What I'm stuck on is that I've got a list of pairs of dates that I want to explode into a list of all the dates between them. Example:

{2010-05-06, 2010-05-09}, {2010-05-12, 2010-05-15}

should result in

{2010-05-06, 2010-05-07, 2010-05-08, 2010-05-09, 2010-05-12, 2010-05-13, 2010-05-14, 2010-05-15}

Note the pairs of dates are guaranteed not to overlap each other.

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ