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

Posted T丶MELO

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于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分组计数排序的实例的主要内容,如果未能解决你的问题,请参考以下文章

用 Pandas 计数和排序

分组,计数,然后排序的最有效方法?

如何按列值的计数进行分组并对其进行排序?

hive 排序 分组计数后排序 几种不同函数的效果

查找与排序:计数排序

具有分组和按日期排序的复杂 sum()