[英]How can I rewrite the following SQL query into LINQ query?
我想在 LINQ 中重寫以下 SQL 查詢。 但我的問題是我不知道如何使用 LINQ LEFT JOIN 編寫 AND (&&) 運算符(看看我的第二個左連接)。
SELECT emp.EmployeeId,
dsg.Name,
pob.CompanyContribution,
pob.EmployeeContribution,
pob.OpeningIncome
FROM HrmEmployees AS emp
LEFT JOIN HrmDesignations AS dsg ON emp.HrmDesignationId=dsg.Id
LEFT JOIN PfmOpeningBalance AS pob ON emp.Id=pob.HrmEmployeeId AND pob.CmnCalendarYearId=2
WHERE emp.Id=6
我嘗試了以下一種。 但是得到編譯錯誤-
from emp in dbContext.EmployeeList
join dsg in dbContext.hrmDesig on emp.HrmDesignationId equals dsg.Id into DSGLeftJoin
from dsglj in DSGLeftJoin.DefaultIfEmpty()
join pob in dbContext.PfOpeningBalances on emp.Id equals pob.HrmEmployeeId into POBLeftJoin
from poblj in POBLeftJoin.DefaultIfEmpty() && poblj.CmnCalendarYearId == clndrId
where emp.Id==empId
select new
{
empIdr = emp.Id,
EmployeeId = emp.EmployeeId,
EmployeeName = emp.Name,
Designation = dsglj.Name,
OpeningIncome = poblj.OpeningIncome,
EmployeeContribution = poblj.EmployeeContribution,
CompanyContribution = poblj.CompanyContribution
}
嘗試這個。
(from emp in HrmEmployees
join dsg in HrmDesignations
on emp.HrmDesignationId equals dsg.Id
join pob in PfmOpeningBalance
on emp.Id equals pob.HrmEmployeeId AND pob.CmnCalendarYearId equals 2
into eGroup
from emps in eGroup.DefaultIfEmpty()
emp.Id=6
select new
{
EmployeeId =emp.EmployeeId,
Name=dsg.Name,
CompanyContribution=pob.CompanyContribution,
EmployeeContribution=pob.EmployeeContribution,
OpeningIncome=pob.OpeningIncome
}).ToList();
好的,你可以這樣試試;
from emp in dbContext.EmployeeList
join dsg in dbContext.hrmDesig on emp.HrmDesignationId=dsg.Id
join pob in dbContext.PfOpeningBalanceson new {emp.Id, jp=pob.CmnCalendarYearId} equals new {pob.HrmEmployeeId, jp=2}
where emp.Id=6
select new {emp.EmployeeId,
dsg.Name,
pob.CompanyContribution,
pob.EmployeeContribution,
pob.OpeningIncome};
或者像這樣;
from emp in dbContext.EmployeeList.Where(e=>e.Id.Equals(6))
join dsg in dbContext.hrmDesig on emp.HrmDesignationId=dsg.Id
join pob in dbContext.PfOpeningBalanceson.Where(x=>x.CmnCalendarYearId.Equals(2)) on emp.Id equals pob.HrmEmployeeId
select new {emp.EmployeeId,
dsg.Name,
pob.CompanyContribution,
pob.EmployeeContribution,
pob.OpeningIncome};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.