我想按employeeId對員工的加班時間進行分組,并獲取employeeNames的資料和員工每月的總加班時間。但是這個 linq 查詢給出了翻譯錯誤。
var results =
from overtime in context.Overtimes
join employeeCredential in context.EmployeeCredentials
on overtime.EmployeeId equals employeeCredential.id
join employeeDetail in context.EmployeeDetails
on employeeCredential.id equals employeeDetail.employeeId
where overtime.Month == month && overtime.Year == year
group new { overtime, employeeCredential, employeeDetail } by overtime.EmployeeId into g
select new MonthlyOvertimeWorkHours
{
EmployeeName = g.First().employeeDetail.employeeName,
TotalWorkHourOfMonth = g.Sum(t => t.overtime.OvertimeWorkHour)
};
uj5u.com熱心網友回復:
在 EF Core 6 之前,您無法在分組后訪問記錄(這通常很糟糕)。僅聚合且Key可用。
要解決問題,只需添加employeeName到分組鍵。
var results =
from overtime in context.Overtimes
join employeeCredential in context.EmployeeCredentials
on overtime.EmployeeId equals employeeCredential.id
join employeeDetail in context.EmployeeDetails
on employeeCredential.id equals employeeDetail.employeeId
where overtime.Month == month && overtime.Year == year
group new { overtime, employeeCredential, employeeDetail } by new { overtime.EmployeeId, employeeDetail.employeeName } into g
select new MonthlyOvertimeWorkHours
{
EmployeeName = g.Key.employeeName,
TotalWorkHourOfMonth = g.Sum(t => t.overtime.OvertimeWorkHour)
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/388622.html
