1、数据分组求合,分别用的实体类以及datatable来分组求合,还有分组求和之后的如何取值
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 //实体类版本 2 List<ProgramTimeModel> TotalAllList = GetData(); 3 4 var a = from p in TotalAllList.AsEnumerable() 5 group p by p.ProgramTime_ID into g 6 select new ProgramTimeModel 7 { 8 ProgramTime_ID = g.Key, 9 Saled = g.Sum(p => p.Saled), 10 ComeIn = g.Sum(p => p.ComeIn) 11 }; 12 13 //拿数据 14 if (a != null && a.ToList().Count > 0) 15 { 16 //方式1 17 List<ProgramTimeModel> TaList = new List<ProgramTimeModel>(); 18 foreach (ProgramTimeModel item in a) 19 { 20 TaList.Add(item); 21 } 22 23 //方式2 24 a.ToList().ForEach(p => 25 { 26 TaList.Add(p); 27 } 28 ); 29 } 30 31 32 //DataTable版本 33 DataTable dtList = GetDataTable(); 34 var b = from r in dtList.AsEnumerable() 35 group r by r.Field<long>("ProgramTime_ID") into g 36 select new 37 { 38 ProgramTime_ID = g.Key, 39 Saled = g.Sum(n => n.Field<int>("Saled")), 40 ComeIn = g.Sum(n => n.Field<int>("ComeIn")) 41 }; 42 43 //拿数据1 44 if (b != null && b.ToList().Count > 0) 45 { 46 //方式1 47 DataTable dt = dtList.Clone(); 48 foreach (var item in b) 49 { 50 DataRow dr = dt.NewRow(); 51 dr["ProgramTime_ID"] = item.ProgramTime_ID; 52 dr["Saled"] = item.Saled; 53 dr["ComeIn"] = item.ComeIn; 54 dt.Rows.Add(dr); 55 } 56 57 //方式2 58 b.ToList().ForEach(p => 59 { 60 DataRow dr = dt.NewRow(); 61 dr["ProgramTime_ID"] = p.ProgramTime_ID; 62 dr["Saled"] = p.Saled; 63 dr["ComeIn"] = p.ComeIn; 64 dt.Rows.Add(dr); 65 } 66 ); 67 }
2、用到的datatable测试数据
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public DataTable GetDataTable() 2 { 3 DataTable dt = new DataTable(); 4 dt.Columns.Add("ProgramTime_ID", typeof(long)); 5 dt.Columns.Add("ProgramTime_Name", typeof(string)); 6 dt.Columns.Add("ProgramTime_Time", typeof(DateTime)); 7 dt.Columns.Add("Saled", typeof(int)); 8 dt.Columns.Add("SaleNoStart", typeof(int)); 9 dt.Columns.Add("ComeIn", typeof(int)); 10 11 DataRow dr = dt.NewRow(); 12 dr["ProgramTime_ID"] = 1; 13 dr["ProgramTime_Name"] = "场次二"; 14 dr["ProgramTime_Time"] = Convert.ToDateTime("2016-08-12 12:20"); 15 dr["Saled"] = 21; 16 dr["SaleNoStart"] = 14; 17 dr["ComeIn"] = 11; 18 dt.Rows.Add(dr); 19 20 dr = dt.NewRow(); 21 dr["ProgramTime_ID"] = 2; 22 dr["ProgramTime_Name"] = "场次三"; 23 dr["ProgramTime_Time"] = Convert.ToDateTime("2016-08-13 12:20"); 24 dr["Saled"] = 221; 25 dr["SaleNoStart"] = 124; 26 dr["ComeIn"] = 121; 27 dt.Rows.Add(dr); 28 29 dr = dt.NewRow(); 30 dr["ProgramTime_ID"] = 3; 31 dr["ProgramTime_Name"] = "场次四"; 32 dr["ProgramTime_Time"] = Convert.ToDateTime("2016-08-14 12:20"); 33 dr["Saled"] = 231; 34 dr["SaleNoStart"] = 134; 35 dr["ComeIn"] = 131; 36 dt.Rows.Add(dr); 37 38 dr = dt.NewRow(); 39 dr["ProgramTime_ID"] = 3; 40 dr["ProgramTime_Name"] = "场次四"; 41 dr["ProgramTime_Time"] = Convert.ToDateTime("2016-08-14 12:20"); 42 dr["Saled"] = 231; 43 dr["SaleNoStart"] = 134; 44 dr["ComeIn"] = 131; 45 dt.Rows.Add(dr); 46 47 return dt; 48 }
3、实体类的测试数据
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public List<ProgramTimeModel> GetData() 2 { 3 List<ProgramTimeModel> list = new List<ProgramTimeModel>(); 4 list.Add(new ProgramTimeModel() { ProgramTime_ID = 1, ProgramTime_Name = "场次一", ProgramTime_Time = Convert.ToDateTime("2016-08-09 12:20"), Saled = 1100, SaleNoStart = 1165, ComeIn = 61237 }); 5 list.Add(new ProgramTimeModel() { ProgramTime_ID = 2, ProgramTime_Name = "场次二", ProgramTime_Time = Convert.ToDateTime("2016-08-12 12:20"), Saled = 83, SaleNoStart = 62, ComeIn = 67 }); 6 list.Add(new ProgramTimeModel() { ProgramTime_ID = 3, ProgramTime_Name = "场次三", ProgramTime_Time = Convert.ToDateTime("2016-08-20 12:20"), Saled = 12, SaleNoStart = 456, ComeIn = 67 }); 7 list.Add(new ProgramTimeModel() { ProgramTime_ID = 3, ProgramTime_Name = "场次三", ProgramTime_Time = Convert.ToDateTime("2016-08-20 12:20"), Saled = 12, SaleNoStart = 456, ComeIn = 67 }); 8 return list; 9 }
注:方式一或方式二只需要取一种