开发者

using GroupBy to get Employee Name, EmployeeID, sum by Pay by category using LINQ

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.Wher开发者_如何学Pythone(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)
                  };

With a loop i can be able to get below without the employeename which is contained in the employee table

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

how do i get the EmployeeName from above?


I would group by EmployeePayDetail.Employee which is what you actually need, then:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜