我需要連接兩個表并使用 linq 從其中一個表中獲取資料。
連接作業正常,但它回傳 IEnumerable <$IEnumerable><$Ojbect> 并且我需要 IEnumerable<$Object>
這是代碼:
(
from med in meds
join pres in prescriptions
on med.Code equals pres.Code into latstPrescrptions
let allPrescriptions = latstPrescrptions.Where(
pres => pres.PatientId == patientId
&& (pres.PrescriptionEndDate > DateTime.Now)
|| pres.PrescriptionEndDate == null
)
select allPrescriptions
);
謝謝
uj5u.com熱心網友回復:
通常,當您想要“展平”集合時,SelectMany您正在尋找的構造。
在 LINQ 查詢語法中,這是通過多個from子句完成的:
from med in meds
join pres in prescriptions
on med.Code equals pres.Code into latstPrescrptions
let allPrescriptions = latstPrescrptions.Where(
pres => pres.PatientId == patientId
&& (pres.PrescriptionEndDate > DateTime.Now)
|| pres.PrescriptionEndDate == null
)
from prescription in allPrescriptions
select prescription
或者,更簡單地說:
from med in meds
join pres in prescriptions
on med.Code equals pres.Code into latstPrescrptions
from prescription in latstPrescrptions.Where(
pres => pres.PatientId == patientId
&& (pres.PrescriptionEndDate > DateTime.Now)
|| pres.PrescriptionEndDate == null
)
不過,更仔細地查看您的查詢,您可能只是避免使用該into子句并依靠join輸出來為您提供所需的內容。
from med in meds
join pres in prescriptions
on med.Code equals pres.Code
where pres.PatientId == patientId
&& (pres.PrescriptionEndDate > DateTime.Now)
|| pres.PrescriptionEndDate == null
select pres
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/449602.html
上一篇:SQLServer資料冗余
