Linq怎样取按月份GroupBy的数据
因为添加日期adddate是时间格式。我想转换成月份:
from b in db.BDMonth group b by b.AddDate.Value.GetDateTimeFormats('y')[0].ToString() into t select new { m = t.FirstOrDefault().AddDate.Value.GetDateTimeFormats('y')[0].ToString(), num = t.Count() };
我知道,linq里不能有方法。我实在想不出来了。
是不是只能自己写方法了。
------解决方案--------------------from b in db.BDMonth
group b by new{b.AddDate.Value.Year ,b.AddDate.Value.Month} into t
select new { m = t.Key.Year+"/"+t.Key.Month, num = t.Count() };
------解决方案--------------------就你目前写的这个,也没必要这么麻烦:
from b in db.BDMonth group b by b.AddDate.Value.GetDateTimeFormats('y')[0].ToString() into t select new { m = t.Key,num = t.Count() };
------解决方案--------------------from b in db.BDMonth
group b by new{b.AddDate.Value.Year ,b.AddDate.Value.Month} into t
select new { m = t.Key.Year+"/"+t.Key.Month, num = t.Count() };