使用GroupBy使用LINQ获取Employee Name、EmployeeID、sum by Pay by cate

本文关键字:使用 by cate Pay sum EmployeeID Name GroupBy LINQ 获取 Employee | 更新日期: 2023-09-27 17:57:39

List<ps_Employee> _Employees = Employee.GetListOfActiveEmployees();
List<ps_Detail> _Details  = Detail.GetListOfDetails();
var _RowDetails = from _Detail in _Details
                  join _Employee in _Employees on _Detail.EmployeeCode equals _Employee.EmployeeCode select new { Employee = _Employee, Detail = _Detail } into EmployeePayDetail
                  group EmployeePayDetail by EmployeePayDetail.Detail.EmployeeCode into x
                  select new
                  {
                      EmployeeCode = x.Key,
                      BasicPay = x.Where(i => i.Detail.ComponentCode.Equals("PE01")).Sum(a => a.Detail.Amount),
                      OtherIncome = x.Where(i => i.Detail.ComponentCode.StartsWith("PE")
                                            && !i.Detail.ComponentCode.Equals("PE01")).Sum(a => a.Detail.Amount),
                      Deductions = x.Where(i => i.Detail.ComponentCode.StartsWith("PD")).Sum(a => a.Detail.Amount),
                      GrossPay = x.Where(i => i.Detail.ComponentCode.StartsWith("PE")).Sum(a => a.Detail.Amount),
                      PAYE = x.Where(i => i.Detail.ComponentCode.Equals("PD03")).Sum(a => a.Detail.Amount),
                      NetPay = x.Where(i => i.Detail.ComponentCode.Equals("PE01")).Sum(a => a.Detail.Amount)
                  };

有了一个循环,我就可以在没有employeename的情况下到达下面,employeena包含在employee表中

employeeCode EmployeeName PEO1    PD 
x001           ?????      200.00  400.00

如何从上面获取EmployeeName?

使用GroupBy使用LINQ获取Employee Name、EmployeeID、sum by Pay by cate

我会按EmployeePayDetail.Employee分组,这是您实际需要的,然后:

select new
{
    EmployeeCode = x.Key.EmployeeCode,
    EmployeeName = x.Key.EmployeeName,
    BasicPay = x.Where(i => i.Detail.ComponentCode.Equals("PE01")).Sum(a => a.Detail.Amount),
    ...