[英]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.