/// <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);
}
}
}
}
运行结果如下: