C# 月份的Linq列表+;全年
正在寻找linq查询,以使用月份+年份填充列表,例如(2012年1月) 从本月开始C# 月份的Linq列表+;全年,c#,linq,C#,Linq,正在寻找linq查询,以使用月份+年份填充列表,例如(2012年1月) 从本月开始 var currentdate = System.DateTime.Now 如果2011年12月为当月 那么列表应该是这样的 2011年12月 2012年1月 ...... 2012年11月我正在编辑我的示例代码,以将其转换为一种我几乎可以在生产中使用的方法,因为它更易于测试且更具文化意识: var months = Enumerable.Range(0, 12). Select(n =
var currentdate = System.DateTime.Now
如果2011年12月为当月
那么列表应该是这样的
2011年12月
2012年1月
......
2012年11月我正在编辑我的示例代码,以将其转换为一种我几乎可以在生产中使用的方法,因为它更易于测试且更具文化意识:
var months =
Enumerable.Range(0, 12).
Select(n => DateTime.Today.AddMonths(n)).
Select(d = new { Year = d.Year, Month = d.Month });
public IEnumerable GetMonths(DateTime currentDate, IFormatProvider provider)
{
return from i in Enumerable.Range(0, 12)
let now = currentDate.AddMonths(i)
select new
{
MonthLabel = now.ToString("MMMM", provider),
Month = now.Month,
Year = now.Year
};
}
此输出(在法国计算机上):