My code here:
var query = context.Article.GroupBy(x=>x.CreateDate.Value.Month)
is not working
For example
January 2017
February 2017
March 2017
My code here:
var query = context.Article.GroupBy(x=>x.CreateDate.Value.Month)
is not working
For example
January 2017
February 2017
March 2017
You can group by your collection using month and year and then loop through the grouped items.
var articlesGrouped = context.Article
.Where(g=>g.CreatedTime!=null)
.GroupBy(x => new { Month = x.CreatedTime.Value.Month,
Year = x.CreatedTime.Value.Year })
.ToList();
This will give you the articles grouped by month and year.
The Month value is the number. If you want the correspnding name, you can use a DateTimeFormatInfo
object.
var dtfi = new DateTimeFormatInfo();
foreach (var groupedItem in postGrouped)
{
var month = dtfi.GetAbbreviatedMonthName(groupedItem.Key.Month)
var year = groupedItem.Key.Year;
//now you can loop through item
foreach (var article in groupedItem)
{
var title = article.Title;
}
}