Linq表达式多个左外连接错误

时间:2013-08-21 14:59:32

标签: linq sql-to-linq-conversion

我无法执行以下linq。

   var items(  from p in Patients 
    join q in MURWorksheets on p.PatientId equals q.PatientId into step1
    from s in step1.DefaultIfEmpty()
    join t in MURWorksheetAnswers on s.MURWorksheetId equals t.MURWorksheetId into step2


    from s2 in step2.DefaultIfEmpty()

    select new {p.FirstName , Date = (s.MURDate == null ? DateTime.Now.Date : s.MURDate),
    s2.MURQuestionnaireId,s2.MURExpctedAnswersId}).ToList();

以下是sql供您参考。

select a.FirstName,b.MURDate,c.MURQuestionnaireId,c.MURWorksheetAnswersID from Patients as a
left join MURWorksheet as b on a.PatientId = b.PatientId
left join MURWorksheetAnswers as c on b.MURWorksheetId = c.MURWorksheetId

1 个答案:

答案 0 :(得分:0)

您在粘贴的代码中缺少等号:

var items = (from p in Patients 
    join q in MURWorksheets on p.PatientId equals q.PatientId into step1
    from s in step1.DefaultIfEmpty()
    join t in MURWorksheetAnswers on s.MURWorksheetId equals t.MURWorksheetId into step2


    from s2 in step2.DefaultIfEmpty()

    select new {p.FirstName , Date = (s.MURDate == null ? DateTime.Now.Date : s.MURDate),
    s2.MURQuestionnaireId,s2.MURExpctedAnswersId}).ToList();