/// <summary>
/// 根據年月進行分組 數據庫日期格式爲 yyyy-MM-dd HH:mm:ss
/// </summary>
public static void YearMonth()
{
using (ZDPORTALEntities db=new ZDPORTALEntities())
{
//Expression<Func<News,object>> w=t=>new{Year=t.N_ReleaseDate.Value.Year,Month=t.N_ReleaseDate.Value.Month};
var q = db.News.GroupBy(t=>new{Year=t.N_ReleaseDate.Value.Year,Month=t.N_ReleaseDate.Value.Month});
var result = q.Select(t => new { Keys = t.Key.Year + "-" + (t.Key.Month.ToString().Length >= 2 ? t.Key.Month.ToString() : "0" + t.Key.Month.ToString())});
foreach (var item in result)
{
Console.WriteLine(item.Keys);
}
}
}
}
運行結果如下: