Linq使用group by

代码:

DataTable dt = ClassWorkInfoBLL.GetStuWorkList(UserID, PageIndex, PageSize);
                var data = from item in dt.AsEnumerable()
                           group item by new { t1 = item.Field<string>("CreateDate") } into m
                           select new
                           {
                               CreateDate = Convert.ToDateTime(m.First().Field<string>("CreateDate")).ToString("yyyy年MM月dd日"),
                               data = from c in m
                                      select new
                                      {
                                          ID = c.Field<int>("ID"),
                                          Name = c.Field<string>("Name"),
                                          IcoKey = c.Field<string>("IcoKey"),
                                          PubDate = Convert.ToDateTime(c.Field<DateTime>("PubDate")).ToString("yyyy-MM-dd HH:mm:ss"),
                                          EndDate = Convert.ToDateTime(c.Field<DateTime>("EndDate")).ToString("yyyy-MM-dd HH:mm:ss"),
                                          Description = c.Field<string>("Description"),
                                          IsDone = c.Field<object>("IsDone"),//类型是:Int64;可能因为是64位系统?
                                          ClassID = c.Field<int>("ClassID"),
                                          TypeID = c.Field<int>("TypeID"),//作业类型 照片=1,视频=2;
                                      }
                           };

var res = query.GroupBy(x => new
{
x.Project,
x.BYear,
x.BMonth
}).Select(x => new
{
x.Key.Project,
x.Key.BYear,
x.Key.BMonth,
Amount = x.Sum(a => a.Amount)
});

原文地址:https://www.cnblogs.com/xsj1989/p/8317410.html