Exploding a range of dates with LINQ
- by Robert Gowland
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.