更新时间:2023-01-30 09:02:19
您可以使用月份和年份对集合进行分组,然后循环浏览分组的项目.
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.
月份值是数字.如果需要相应的名称,可以使用DateTimeFormatInfo
对象.
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;
}
}