我有一个带有属性FlyerDate作为日期时间的传单集合,我想创建一个月和年的下拉列表,例如"nov 2015,2015年12月,2016年1月"......
这是我的代码:
var monthList = flyers.Where(i => i.FlyerDate != DateTime.MinValue && i.FlyerDate.Year >= 2013)
.GroupBy(i => i.FlyerDate.Month)
.Select(g => new {
Month = g.Key,
Year = g.First(i => i.FlyerDate != DateTime.MinValue).FlyerDate.Year,
FullDate = String.Concat(DateTimeFormatInfo.CurrentInfo.GetMonthName(g.Key), " ", g.First(i => i.FlyerDate != DateTime.MinValue).FlyerDate.Year),
Total = g.Count(i => i.FlyerID > 0)
}
);
Run Code Online (Sandbox Code Playgroud)
我希望GroupBy月份和年份的工作在我的情况下,列表仅包含每个月的第一次出现.任何提示?
您需要按包含年份和月份的匿名类型进行分组:
var monthList = flyers.Where(i => i.FlyerDate.Year >= 2013)
.GroupBy(i => new { i.FlyerDate.Year, i.FlyerDate.Month })
.Select(g => new {
Year = g.Key.Year,
Month = g.Key.Month,
FullDate = DateTimeFormatInfo.CurrentInfo.GetMonthName(g.Key.Month) + " " + g.Key.Year
});
Run Code Online (Sandbox Code Playgroud)
顺便说一下,如果你想要缩写的月份名称作为你想要的结果,那么建议你需要使用DateTimeFormatInfo.CurrentInfo.GetAbbreviatedMonthName而不是GetMonthName.
| 归档时间: |
|
| 查看次数: |
575 次 |
| 最近记录: |