我需要连接两个表并使用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
);谢谢
发布于 2022-03-23 16:52:05
通常,当您想要“平整”一个集合时,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 preshttps://stackoverflow.com/questions/71590784
复制相似问题