【问题标题】:Get the total count per department获取每个部门的总数
【发布时间】:2018-05-18 10:23:17
【问题描述】:

这个问题是Count Duplicates的链接

我有这样的输出:

Department    Name     Count
Sales         John     2
Sales         Peter    1
Admin         Jack     1
Admin         Sophia   2
Admin         Jill     1
Maintenance   Bob      2
Maintenance   Henry    1

我想用这个信息输出的是:

Department    Name     Count
Sales         John     2
Sales         Peter    1
------------------------
Total                  3
------------------------
Admin         Jack     1
Admin         Sophia   2
Admin         Jill     1
------------------------
Total                  4
------------------------
Maintenance   Bob      2
Maintenance   Henry    1
------------------------
Total                  3
------------------------

我正在使用以下代码输出第一个列表:

var query = data.AsEnumerable()
                .GroupBy(r => new { Dept = r.Field<string>("Department"),
                    Name = r.Field<string>("FirstName"),
                    Surname = r.Field<string>("LastName")})
                .Select(grp => new
                {
                    Dept = grp.Key.Dept,
                    Name = grp.Key.Name,
                    Surname = grp.Key.Surname,
                    Count = grp.Count()
                });

            foreach(var item in query)
            {
                newData.Rows.Add(item.Dept, item.Name, item.Surname, item.Count);
            }

如何添加每个部门的总数?

【问题讨论】:

    标签: .net visual-studio linq datatable


    【解决方案1】:
    data.AsEnumerable()
      .GroupBy(r => r.Field<string>("Department"))
      .ToDictionary(g => g.Key, g => g.Count());
    

    这将为您提供Dictionary&lt;string, int&gt;,其中键是部门名称,值是计数。

    【讨论】:

      猜你喜欢
      • 2016-06-15
      • 2021-03-28
      • 1970-01-01
      • 2021-12-14
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多