You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中为DataTable的每个部门添加总计数?

为DataTable的每个部门添加总计数

Hey, let's get those department total rows added to your output! From what you've shared, you're already grouping by department and name to count duplicates, but just need to add a summary line for each department.

当前输出

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);
}

解决方案

The trick here is to first group your results by department after getting the individual person counts. That way, you can process each department's entries, then add the total line for that group.

Here's how to adjust your code:

// Step 1: Get individual person counts grouped by department + name
var personCounts = data.AsEnumerable()
    .GroupBy(r => new 
    { 
        Dept = r.Field<string>("Department"), 
        FirstName = r.Field<string>("FirstName"), 
        LastName = r.Field<string>("LastName")
    })
    .Select(grp => new 
    { 
        Dept = grp.Key.Dept,
        FirstName = grp.Key.FirstName,
        LastName = grp.Key.LastName,
        Count = grp.Count() 
    });

// Step 2: Group those person counts by department to handle each section
var departmentGroups = personCounts.GroupBy(p => p.Dept);

foreach (var deptGroup in departmentGroups)
{
    // Add all the person rows for this department
    foreach (var person in deptGroup)
    {
        newData.Rows.Add(person.Dept, person.FirstName, person.LastName, person.Count);
    }

    // Calculate the total count for the department (sum of all person counts)
    int deptTotal = deptGroup.Sum(p => p.Count);

    // Add separator line (match your desired format)
    newData.Rows.Add("------------------------", "", "", "");
    // Add total row
    newData.Rows.Add("Total", "", "", deptTotal);
    // Add another separator line
    newData.Rows.Add("------------------------", "", "", "");
}

说明

  • We first generate the individual person counts just like your original code, then wrap that in a second grouping by department.
  • For each department, we first write all the person entries, then calculate the sum of all Count values in that department for the total.
  • The separator lines are added as rows with the dashed string in the Department column (adjust this if your DataTable schema needs different handling).

内容的提问来源于stack exchange,提问作者JohnWayne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:52:53