0

SQLクエリをLINQに「変換」しようとしていますが、これまで成功していません。

これは私のSQLクエリで、null値を持つ複数の列を持つ1つの結果を返します(正しい出力):

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でLEFTOUTERJOINSをコーディングする方法を示しています。

于 2013-03-05T00:12:34.433 に答える