首页 > 代码库 > 关于dt分组、计数、排序的实例

关于dt分组、计数、排序的实例

 1  #region table去重复求和
 2                     var query = dt.Rows.Cast<DataRow>()
 3                         .OrderByDescending(n => n["OPERATION_TIME"])
 4                       .GroupBy(
 5                      s => new
 6                      {
 7                          MATERIAL_CODE = Convert.ToString(s["MATERIAL_CODE"]),
 8                          MATERIAL_NAME = Convert.ToString(s["MATERIAL_NAME"]),
 9                          STORAGE_STYLE = Convert.ToString(s["STORAGE_STYLE"]),
10                          STORAGE_SIGN = Convert.ToString(s["STORAGE_SIGN"])
11                      })
12                      .Select(group => new
13                      {
14                          group.Key.MATERIAL_CODE,
15                          group.Key.MATERIAL_NAME,
16                          group.Key.STORAGE_STYLE,
17                          group.Key.STORAGE_SIGN,
18                          REMARK = group.First().Field<string>("REMARK"),
19                          OPERATION_TIME = group.First().Field<string>("OPERATION_TIME"),
20                          STORAGE_NUM = group.Sum(x => Convert.ToInt32(x["STORAGE_NUM"]))
21                      });
22 
23                     if (query.ToList().Count > 0)
24                     {
25                         dtNew = dt.Clone();
26                         query.ToList().ForEach(q =>
27                         {
28                             DataRow drNew = dtNew.NewRow();
29                             drNew["MATERIAL_CODE"] = q.MATERIAL_CODE;
30                             drNew["MATERIAL_NAME"] = q.MATERIAL_NAME;
31                             drNew["REMARK"] = q.REMARK;
32                             drNew["STORAGE_NUM"] = q.STORAGE_NUM;
33                             drNew["STORAGE_STYLE"] = q.STORAGE_STYLE;
34                             drNew["OPERATION_TIME"] = q.OPERATION_TIME;
35                             drNew["STORAGE_SIGN"] = q.STORAGE_SIGN;
36                             dtNew.Rows.Add(drNew);
37                         });
38                     }
39                     #endregion

 

关于dt分组、计数、排序的实例