[英]Convert this LINQ to Lambda Expression?
我有一个LINQ,它工作正常。 我的问题是:如何将其转换为Lambda Expression?
var searchResults = from study in dataContext.Studies
join location in dataContext.Locations
on study.LocationID equals location.LocationID
join doctorLocation in dataContext.DoctorLocations
on location.LocationID equals doctorLocation.LocationID
join doctor in dataContext.Doctors
on doctorLocation.DoctorID equals doctor.DoctorID
where doctor.DoctorID == doctorId
select study;
我认为LINQ对我来说更自然(类似于SQL脚本)。 但是,在这种情况下,我只想将其转换为Lambda Expression,但我无法使其工作。
我被困在:
var searchResults = dataContext.Studies.Where(x =>
x.Location.DoctorLocations.FirstOrDefault() != null &&
x.Location.DoctorLocations.FirstOrDefault().DoctorID == doctorId);
这仅适用于FirstOrDefault。 由于有多个DoctorLocations,我不知道怎么写这个。
尝试这个:
var searchResults = dataContext.Studies.Where(x =>
x.Location != null
&& x.Location.DoctorLocations.Any(dl => dl.DoctorID == doctorId));
您将获得与DoctorID
等于doctorId
至少一个DoctorLocation
相关的所有Studies
var searchResults = dataContext.Studies
.Include(x => x.Locations)
.Include(x => x.DoctorLocations)
.Include(x => x.Doctors)
.Where(x => x.[InheritedPathToDoctor].DoctorId == id)
.Select(x => x.[InheritedPathToStudy].Study)
.FirstOrDefault() OR .ToList()
关于如何设置上下文,我在这里做了很多假设。 我假设它是一个关系数据库,因此包含只是意味着它返回所有数据。 我没有测试它,所以可能有一些错误。
你需要为每个班级提供一个包含,并且其中的地方非常自我解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.