1、数据分组求合,分别用的实体类以及datatable来分组求合,还有分组求和之后的如何取值

                 //实体类版本
List<ProgramTimeModel> TotalAllList = GetData(); var a = from p in TotalAllList.AsEnumerable()
group p by p.ProgramTime_ID into g
select new ProgramTimeModel
{
ProgramTime_ID = g.Key,
Saled = g.Sum(p => p.Saled),
ComeIn = g.Sum(p => p.ComeIn)
}; //拿数据
if (a != null && a.ToList().Count > )
{
//方式1
List<ProgramTimeModel> TaList = new List<ProgramTimeModel>();
foreach (ProgramTimeModel item in a)
{
TaList.Add(item);
} //方式2
a.ToList().ForEach(p =>
{
TaList.Add(p);
}
);
} //DataTable版本
DataTable dtList = GetDataTable();
var b = from r in dtList.AsEnumerable()
group r by r.Field<long>("ProgramTime_ID") into g
select new
{
ProgramTime_ID = g.Key,
Saled = g.Sum(n => n.Field<int>("Saled")),
ComeIn = g.Sum(n => n.Field<int>("ComeIn"))
}; //拿数据1
if (b != null && b.ToList().Count > )
{
//方式1
DataTable dt = dtList.Clone();
foreach (var item in b)
{
DataRow dr = dt.NewRow();
dr["ProgramTime_ID"] = item.ProgramTime_ID;
dr["Saled"] = item.Saled;
dr["ComeIn"] = item.ComeIn;
dt.Rows.Add(dr);
} //方式2
b.ToList().ForEach(p =>
{
DataRow dr = dt.NewRow();
dr["ProgramTime_ID"] = p.ProgramTime_ID;
dr["Saled"] = p.Saled;
dr["ComeIn"] = p.ComeIn;
dt.Rows.Add(dr);
}
);
}

2、用到的datatable测试数据

 public DataTable GetDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("ProgramTime_ID", typeof(long));
dt.Columns.Add("ProgramTime_Name", typeof(string));
dt.Columns.Add("ProgramTime_Time", typeof(DateTime));
dt.Columns.Add("Saled", typeof(int));
dt.Columns.Add("SaleNoStart", typeof(int));
dt.Columns.Add("ComeIn", typeof(int)); DataRow dr = dt.NewRow();
dr["ProgramTime_ID"] = ;
dr["ProgramTime_Name"] = "场次二";
dr["ProgramTime_Time"] = Convert.ToDateTime("2016-08-12 12:20");
dr["Saled"] = ;
dr["SaleNoStart"] = ;
dr["ComeIn"] = ;
dt.Rows.Add(dr); dr = dt.NewRow();
dr["ProgramTime_ID"] = ;
dr["ProgramTime_Name"] = "场次三";
dr["ProgramTime_Time"] = Convert.ToDateTime("2016-08-13 12:20");
dr["Saled"] = ;
dr["SaleNoStart"] = ;
dr["ComeIn"] = ;
dt.Rows.Add(dr); dr = dt.NewRow();
dr["ProgramTime_ID"] = ;
dr["ProgramTime_Name"] = "场次四";
dr["ProgramTime_Time"] = Convert.ToDateTime("2016-08-14 12:20");
dr["Saled"] = ;
dr["SaleNoStart"] = ;
dr["ComeIn"] = ;
dt.Rows.Add(dr); dr = dt.NewRow();
dr["ProgramTime_ID"] = ;
dr["ProgramTime_Name"] = "场次四";
dr["ProgramTime_Time"] = Convert.ToDateTime("2016-08-14 12:20");
dr["Saled"] = ;
dr["SaleNoStart"] = ;
dr["ComeIn"] = ;
dt.Rows.Add(dr); return dt;
}

3、实体类的测试数据

 public List<ProgramTimeModel> GetData()
{
List<ProgramTimeModel> list = new List<ProgramTimeModel>();
list.Add(new ProgramTimeModel() { ProgramTime_ID = , ProgramTime_Name = "场次一", ProgramTime_Time = Convert.ToDateTime("2016-08-09 12:20"), Saled = , SaleNoStart = , ComeIn = });
list.Add(new ProgramTimeModel() { ProgramTime_ID = , ProgramTime_Name = "场次二", ProgramTime_Time = Convert.ToDateTime("2016-08-12 12:20"), Saled = , SaleNoStart = , ComeIn = });
list.Add(new ProgramTimeModel() { ProgramTime_ID = , ProgramTime_Name = "场次三", ProgramTime_Time = Convert.ToDateTime("2016-08-20 12:20"), Saled = , SaleNoStart = , ComeIn = });
list.Add(new ProgramTimeModel() { ProgramTime_ID = , ProgramTime_Name = "场次三", ProgramTime_Time = Convert.ToDateTime("2016-08-20 12:20"), Saled = , SaleNoStart = , ComeIn = });
return list;
}

注:方式一或方式二只需要取一种

05-03 23:30