【发布时间】:2016-10-13 21:56:12
【问题描述】:
我有一组传单,其属性 FlyerDate 作为日期时间,我想创建一个包含月份和年份的下拉列表,例如“2015 年 11 月、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)
}
);
我希望GroupBy 在月份和年份都有效,因为在我的情况下,列表仅包含每个月的第一次出现。有什么提示吗?
【问题讨论】:
-
i.FlyerDate != DateTime.MinValue由于第二个条件不需要 -
.GroupBy(i => i.FlyerDate.Month * 10000 + i.FlyerDate.Year)