I have a list with datetime objects. I would like to group by month and add it to the dictionary.
So, after the grouping process, I want to have a list of month and year. For example: Before grouping [mix below elements]
After grouping: 1) January 2015 - 5 elements 2) February 2015 - 2 elements 2) January 2014 - 2 elements
I tried like this:
var test = this.myList.GroupBy(x => x.myDate.Month).ToList();
But I have to use a dictionary. Do you have any idea how to solve it?
Linq provides the ability to convert your results into a dictionary:
myList.GroupBy(x => new {Month = x.myDate.Month, Year = x.myDate.Year}) .ToDictionary(g => g.Key, g => g.Count())
, {Month=1,Year=2015}. - January 2015, .
{Month=1,Year=2015}
January 2015