2014-01-25 161 views
0

我有一個LINQ,它工作正常。我的問題是:如何將其轉換爲Lambda表達式?將此LINQ轉換爲Lambda表達式?

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,所以我不會寫這個。

+1

究竟你嘗試過,不能讓工作?請分享它。 –

+0

已在原始帖子中更新。我不熟悉Lamda,所以陷入困境。 – urlreader

回答

2

試試這個:

var searchResults = dataContext.Studies.Where(x => 
    x.Location != null 
    && x.Location.DoctorLocations.Any(dl => dl.DoctorID == doctorId)); 

你會得到相關的至少一個DoctorLocationDoctorID所有Studies等於doctorId

0
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() 

在這裏我已經做了很多關於如何設置上下文的假設。我認爲它是一個關係數據庫,因此包含僅僅意味着它返回所有數據。我還沒有測試過,所以可能有一些錯誤。

你需要一個包括每個類和哪裏是相當自我解釋。

相關問題