0

我正在尝试将 SQL 查询“翻译”为 LINQ,但直到现在都没有成功。

这是我的 SQL 查询,它返回一个结果,其中包含多个具有空值的列(正确输出):

SELECT patient.idPatient, removed, fat.status AS FATstatus, fad.status AS FADstatus,
fa.status AS FAstatus, fdv.status AS FDVstatus, ffu.status AS FFUstatus, fmbct.status
AS FMBCTstatus, fnt.status AS FNTstatus, fs.status AS FSstatus, ftd.status AS FTDstatus  
FROM patient 
LEFT JOIN FormAdjuvantTreatment fat ON patient.idPatient = fat.idPatient
LEFT JOIN FormAdvancedDisease fad ON patient.idPatient = fad.idPatient
LEFT JOIN FormAnamnesis fa ON patient.idPatient = fa.idPatient
LEFT JOIN FormDemoVariables fdv ON patient.idPatient = fdv.idPatient
LEFT JOIN FormFollowUp ffu ON patient.idPatient = ffu.idPatient
LEFT JOIN FormMBCTreatment fmbct ON patient.idPatient = fmbct.idPatient
LEFT JOIN FormNeoadjuvantTreatment fnt ON patient.idPatient = fnt.idPatient
LEFT JOIN FormSurgery fs ON patient.idPatient = fs.idPatient
LEFT JOIN FormTumorDisease ftd ON patient.idPatient = ftd.idPatient
WHERE patient.idResearcher = '01-01';

这是我翻译的 LINQ 查询。我没有返回任何结果(输出不正确):

var query = (from u in db.Patients.DefaultIfEmpty()
join fat in db.FormAdjuvantTreatments on u.idPatient equals fat.idPatient
join fad in db.FormAdvancedDiseases on u.idPatient equals fad.idPatient
join fa in db.FormAnamnesis on u.idPatient equals fa.idPatient
join fdv in db.FormDemoVariables on u.idPatient equals fdv.idPatient
join ffu in db.FormFollowUps on u.idPatient equals ffu.idPatient
join fmbct in db.FormMBCTreatments on u.idPatient equals fmbct.idPatient
join fnt in db.FormNeoadjuvantTreatments on u.idPatient equals fnt.idPatient
join fs in db.FormSurgeries on u.idPatient equals fs.idPatient
join ftd in db.FormTumorDiseases on u.idPatient equals ftd.idPatient
where u.idResearcher == idResearcher
select new
{
   u.idPatient,
   u.removed,
   FormAdjuvantTreatment = (fat.status == null ? "NULL" : fat.status),
   FormAdvancedDisease = (fad.status == null ? "NULL" : fad.status),
   FormAnamnesi = (fa.status == null ? "NULL" : fa.status),
   FormDemoVariable = (fdv.status == null ? "NULL" : fdv.status),
   FormFollowUp = (ffu.status == null ? "NULL" : ffu.status),
   FormMBCTreatment = (fmbct.status == null ? "NULL" : fmbct.status),
   FormNeoadjuvantTreatment = (fnt.status == null ? "NULL" : fnt.status),
   FormSurgery = (fs.status == null ? "NULL" : fs.status),
   FormTumorDisease = (ftd.status == null ? "NULL" : ftd.status),
}).ToList();
4

2 回答 2

1

感谢@lazyberezovsky 和@Pieter-Geerkens 我已经解决了我的问题。

SQL查询(左内连接):

SELECT patient.idPatient, removed, fat.status AS FATstatus
FROM patient 
LEFT JOIN FormAdjuvantTreatment fat ON patient.idPatient = fat.idPatient
WHERE patient.idResearcher = '01-01';

正确翻译的 LINQ 查询(左内连接):

var query = (from u in db.Patients.DefaultIfEmpty()
join fat in db.FormAdjuvantTreatments on u.idPatient equals fat.idPatient into JoinedPatientFAT
from fat in JoinedPatientFAT.DefaultIfEmpty()
where u.idResearcher == idResearcher
select new
{
u.idPatient,
u.removed,
FormAdjuvantTreatment = fat.status ?? "NULL"
}
).ToList();
于 2013-03-05T00:29:13.053 回答
0

此 MSDN 链接演示了如何在 LINQ 中编写 LEFT OUTER JOINS代码:

于 2013-03-05T00:12:34.433 回答