2014-01-25 164 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() 

在这里我已经做了很多关于如何设置上下文的假设。我认为它是一个关系数据库,因此包含仅仅意味着它返回所有数据。我还没有测试过,所以可能有一些错误。

你需要一个包括每个类和哪里是相当自我解释。

相关问题