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

时间:2014-09-08 09:36:19

标签: c# mysql sql linq

我想在其组之后获得许多列的总和,就像这样是我的表

  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
-----------------------------------------------------

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

听起来你需要这样的事情:

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

即。按部门划分,然后为每个小组迭代小组内的雇员,然后是小组的总和。