I have a table called Events and these are example of EndDate columns:

I am trying to extract months from these events, but I want them to be like: 11, 12, 1 (11 and 12 from current year, and 1 is from next year – 2013).
var ev = db.Events.Select(d => new { Month = d.StartDate.Value.Month,
EndDate = d.EndDate })
.Where(d => (d.EndDate >= DateTime.Now
|| ( d.EndDate.Value.Day == DateTime.Now.Day
&& d.EndDate.Value.Month >= DateTime.Now.Month) ))
.OrderBy(d => d.EndDate.Value.Year)
.Select(d => new { Month = d.Month }).Distinct();
Well, I don’t understand why this query does not work. It extracts months as: 1, 11, 12 which of course, is not what I want…
Ps: You can ignore where clause, that is only filtering the events from now on.
You are sorting on Year only
.OrderBy(d => d.EndDate.Value.Year)but you probably want to sort byd.EndDate.ValueYear only will be ignoring any days/months part of the date…so the “first” 2012 in the list is the first item.
will return
12,11,5,4will return
12,4,5,11