使用 GroupBy 获取员工姓名、员工 ID、使用 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.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)
};
通过循环,我可以在没有员工表中包含的员工姓名的情况下获得下面的信息,
employeeCode EmployeeName PEO1 PD
x001 ????? 200.00 400.00
如何从上面获取员工姓名?
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)
};
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将按您实际需要的
EmployeePayDetail.Employee
进行分组,然后:I would group by
EmployeePayDetail.Employee
which is what you actually need, then: