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
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