3
votes

I'm attempting to filter a calendar (a list of dates) by day number, but struggling for a specific scenario!

The calendar would initially contain all dates over a specified range, say 31st Jan 2013 - 31st Jan 2015. I would like to filter this list to contain only dates which match the first day number in the calendar, so for example if the first day in the calendar is 25, the new filtered calendar returned would be:

  • 25 Feb 2013
  • 25 Mar 2013
  • 25 Apr 2013

... etc

This first example is simple enough with LINQ

var calendar = ...code to get calendar from DB.

//Get Day Number of First Entry in Calendar (31st for example)
int day = calendar.Dates.Select(d => d.Date.Day).First();

//Filter the rest of the calendar by this date.
return new Calendar
{
    Dates = calendar.Dates.Where(c => c.Date.Day == day).ToList()
};

I get into difficulty when passing in say, 31. My requirement is so that this is returned:

  • 31 Jan 2013
  • 28 Feb 2013
  • 30 Apr 2013
  • 31 May 2013

... etc

What's the best way to achieve this? It's pecking my brain on a Wednesday morning!

I would like if at all possible for the answer to be a perfect one-liner LINQ statement :)

Many thanks,

Alex

2
Can you provide the year that will be used?It'sNotALie.
So you do not really want the date with the day being 31 but the last day of any month? Maybe this method will help: bool IsLastOfMonth(DateTime date) { return date == new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1); } (not tested)Corak
The pre-filtered calendar will most likely contain a date range spanning 3-5 years, say, 2012 - 2015 with all dates in between. Does that answer your question?Huntsman
Not really, but I forgot about DateTime.Year. Answer incoming!It'sNotALie.
@Corak - Yes, thanks, perhaps a combination of this and the original code may work. I'll see if I can get something mashed up.Huntsman

2 Answers

1
votes

DateTime.DaysInMonth to the rescue!

return new Calendar
{
Dates = calendar.Dates.Where(c => c.Date.Day == Math.Min(day, DateTime.DaysInMonth(c.Year, c.Month))).ToList()
};
1
votes
Dates = calendar.Dates.Where(c => c.Date.Day == Math.Min(day, DateTime.DaysInMonth(c.Year, c.Month))).ToList()