LINQ的清单月+年全年 [英] Linq list for month+year for the whole year
本文介绍了LINQ的清单月+年全年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
寻找LINQ查询,以填补月+年为例名单(2012年1月)
Looking for linq query to fill a list with month + year for example (January 2012)
启动形式当月
var currentdate = System.DateTime.Now
如果2011年12月是当月
,则列表应该是这样的。
If Dec 2011 is the current month then list should be like this
2011年12月
2012年1月
.....
2012年11月
December 2011 January 2012 ...... November 2012
推荐答案
我编辑把我的示例代码到,我几乎可以使用的方法生产,因为它更容易测试和文化意识的:
I'm editing to turn my sample code into a method that I might almost use in production because it's more testable and culture-aware:
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
};
}
这输出(法国计算机上):
This outputs (on a French computer):
这篇关于LINQ的清单月+年全年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文