Linq 在内连接和左外连接上的困难

Difficulty with Linq on inner and left outer joins

我不明白为什么会失败。错误是:

Failure: Identifier 'Patient_recid' is not a parameter or variable or field of 'Nova.Data.Returntooffice'. If 'Patient_recid' is a property please add the FieldAlias or Storage attribute to it or declare it as a field's alias.

是的...没错。 table 的设置使得

  1. Returntooffices 引用 encountertimes 记录(内部联接)。
  2. encountertimes 记录引用 patient table 记录(内连接)。
  3. Returntoooffice 记录可能有也可能没有 appointment 记录 -- 这就是我尝试使用左外连接的原因。

returntooffices 记录没有 patient_recid,这就是我试图将其加入 encountertimes 记录的原因。

这是如何正确完成的?

TIA

var query = 
    from rto in cxt.Returntooffices
    from encounter in cxt.Encountertimes.Where(f => f.Recid == rto.Encounter_recid)
    from patient in cxt.Patients.Where(p => p.Recid == encounter.Patient_recid && p.Groupid == groupid)
    from appointment in cxt.Appointments.Where(a => a.Recid == rto.Appointment_recid).DefaultIfEmpty()
    select new
    {
        RTO = rto,
        APPOINTMENT = appointment
     };

 var b = query.ToList();

代替更好的想法,这似乎可以编译并工作(是的!)

var q = 
    from rto in cxt.Returntooffices
    join encounter in cxt.Encountertimes on rto.Encounter_recid equals encounter.Recid 
    join patient in cxt.Patients on encounter.Patient_recid equals patient.Recid
    join appointment in cxt.Appointments on rto.Appointment_recid equals appointment.Recid into apt
    from a in apt.DefaultIfEmpty() 
    where patient.Groupid == groupid
    select new
     {
        RTO = rto,
        APPOINTMENT = a
      }
    ).ToList();