C# 使用linq获取其组后的列总和

C# 使用linq获取其组后的列总和,c#,mysql,sql,linq,C#,Mysql,Sql,Linq,我想得到它的组后许多列的总和,就像这是我的表 Name | Department | basic salary | EPF | ETF | ------------------------------------------------------- Prasad Head Office 25000 1200 800 sean Head Office 25000 1200 800 -----

我想得到它的组后许多列的总和,就像这是我的表

  Name     |  Department | basic salary | EPF   | ETF  |
 -------------------------------------------------------
 Prasad      Head Office     25000        1200    800
 sean        Head Office     25000        1200    800
------------------------------------------------------
Total                        50000        2400    1600  // I want To get This Row In between every                   
------------------------------------------------------     Department Chage.How to add this row
 John        X1              30000        1500    950
 karl        x1              20000        1000    700
 mena        x1              10000         500    250
-----------------------------------------------------
Total                        60000        3000   1900
-----------------------------------------------------

尝试下面的查询可能会对您有所帮助

select isnull(e.EmpName,'Total'),D.Dname Department,
sum(E.BasicSalary),sum(E.EPF),sum(E.ETF)
from Employee E inner join Department D on D.DeptNo=E.DeptNo
group by  D.Dname,e.EmpName with rollup

听起来你需要这样做:

foreach(var grp in employees.GroupBy(x => x.Department)) 
{
  foreach(var emp in grp)
  {
     Console.WriteLine(String.Join("\t", emp.Name, emp.Department, emp.BasicSalary, emp.EPF, emp.ETF));
  }
  Console.WriteLine(String.Join("\t", "Total", "", grp.Sum(x => x.BasicSalary), grp.Sum(x => x.EPF), grp.Sum(x => x.ETF)));
}

i、 e.按部门分组,然后对每个组迭代该组内的员工,然后是该组的总和。

您希望如何做到这一点?它是SQL还是C代码?请告诉我们你们尝试了什么类似的问题,不确定答案是否有用-