EF连接多个表并显示多个数据集

时间:2018-03-27 14:55:10

标签: c# entity-framework

患者表与医生表有一对多的关系。 我怎样才能将doctorNoteID 3和4一起取回我的人物结果?请看下面的附图。目前我只能获取一个结果,即doctornoteID 3。

    public IHttpActionResult testing(int patientID, string token)
    {
        var person = (from p in _context.Patients
                      join e in _context.PatientAllocations
                      on p.patientID equals e.patientID
                      join d in _context.DoctorNotes
                      on p.patientID equals d.patientID
                      where p.patientID == patientID
                      select new
                      {
                          patient_patientID = p.patientID,
                          patient_firstName = p.firstName,
                          patient_lastName = p.lastName,
                          patientallocation_patientAllocationID = e.patientAllocationID,
                          patientallocation_patientID = e.patientID,
                          DoctorNote_doctorNoteID = d.doctorNoteID,
                          DoctorNote_doctorNote = d.note,
                      }).ToList();
        return Ok(person);
    }

enter image description here

患者模型

public class Patient
{
    [Required]
    public int patientID { get; set; }

    [StringLength(255)]
    public string firstName { get; set; }

    [StringLength(255)]
    public string lastName { get; set; }
}

DoctorNote

public class DoctorNote
{
    [Required]
    public int doctorNoteID { get; set; }
    public string note { get; set; }
    public Patient Patient { get; set; }
    public int patientID { get; set; }

}

1 个答案:

答案 0 :(得分:1)

而不是像你一样手动加入:

public IHttpActionResult testing(int patientID, string token)
{
    var person = (from p in _context.Patients
                  join e in _context.PatientAllocations
                  on p.patientID equals e.patientID
                  join d in _context.DoctorNotes
                  on p.patientID equals d.patientID
                  where p.patientID == patientID
                  select new
                  {
                      patient_patientID = p.patientID,
                      patient_firstName = p.firstName,
                      patient_lastName = p.lastName,
                      patientallocation_patientAllocationID = e.patientAllocationID,
                      patientallocation_patientID = e.patientID,
                      DoctorNote_doctorNoteID = d.doctorNoteID,
                      DoctorNote_doctorNote = d.note,
                  }).ToList();
    return Ok(person);
     }

你可以试试这个: 假设导航属性的名称与集合......

相同
public IHttpActionResult testing(int patientID, string token)
 {
    var person = Context.Patients
                          .AsNoTracking()
                          .Include(p=>p.PatientAllocations)
                          .Include(d=>d.DoctorNotes)
                           .Where(p=>p.PatientID==patientID)
                           .ToList();

   return Ok(person);
 }

编辑:

像这样改变你的患者类,那么你的问题应该消失了:

public class Patient
{
    [Required]
    public int patientID { get; set; }

    [StringLength(255)]
    public string firstName { get; set; }

    [StringLength(255)]
    public string lastName { get; set; }

    public virtual ICollection<DoctorNote> DoctorNotes { get; set;}
    public virtual ICollection<PatientAllocation> PatientAllocations { get; set; }
}