嘗試......
class Program
{
static void Main(string[] args)
{
var dateTimes = new[]
{
new DateTime(2016, 06, 29),
new DateTime(2016, 06, 27),
new DateTime(2016, 05, 05),
new DateTime(2016, 04, 15),
new DateTime(2016, 04, 13)
};
var years = dateTimes.GroupBy(x => x.Year).OrderByDescending(x => x.Key);
foreach (IGrouping<int, DateTime> grouping in years)
{
var months = grouping.GroupBy(x => x.Month);
foreach (IGrouping<int, DateTime> month in months)
{
Console.WriteLine(month.First());
}
}
Console.ReadLine();
}
}
此輸出以下...
29/06/2016 00:00:00
05/05/2016 00:00:00
15/04/2016 00:00:00
來源
2016-07-07 07:19:41
Jay
我會計算每個月有多少天了,然後檢查每月這是最新的日期。 – user743414
按年/月分組日期時間,並從每個組中選擇最大值 – Philippe