tags:

views:

88

answers:

2

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.

A: 

I think you could use a simple List<DateTime> to accomplish what you want.

List<DateTime> list = new List<DateTime>();
list.AddRange(GetListDates(d1, d2));
list.AddRange(GetListDates(d3, d4));
Paulo Santos
+2  A: 
var result = listOfPairs.SelectMany(pair =>
                 Enumerable.Range(0, (pair.Item2 - pair.Item1).TotalDays)
                           .Select(days => pair.Item1.AddDays(days)));
dtb
This looks good if the 'return' is dropped.
Robert Gowland
Answer updated to have no 'return' :)
dtb
Yes, this is what I was looking for: a LINQy extensible method of doing. Awesome, thanks.
Robert Gowland