Retrieve specific days of the week within a given range from Linq?

c#

Solution

You can determine the number of days between the dates, then generate all the dates and filter on the days of the week you're interested in.

var start = DateTime.Parse("08/17/2012");
var end = DateTime.Parse("09/17/2012");
int numberOfDays = end.Subtract(start).Days + 1;
var daysOfWeek = new[] { DayOfWeek.Tuesday, DayOfWeek.Thursday };

var dates = Enumerable.Range(0, numberOfDays)
                      .Select(i => start.AddDays(i))
                      .Where(d => daysOfWeek.Contains(d.DayOfWeek));

Problem

Is it possible given a date range to pull out specific days of the week and display their dates? So, if my date range is from 08/17/2012 to 09/17/2012 is it possible, using linq to pull out say, all of the Thursdays and Tuesdays?

Original source