简体   繁体   English

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

[英]EF Joining multiple tables and display multiple data sets

Patient table has a one to many relationship with doctornote table. 患者表与Doctornote表具有一对多关系。 How can i fetch doctorNoteID 3 and 4 together into my person result?? 我如何才能将DoctorNoteID 3和4一起获取到我的人结果中? Look at the attached image below. 请看下面的图片。 Currently i can only fetch one result which is doctornoteID 3. 目前,我只能获取一个结果,即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);
    }

在此处输入图片说明

Patient Model 患者模型

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

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

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

DoctorNote 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; }

}

Instead of joining manually like you try: 而不是像您尝试手动加入:

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);
     }

You could try this: Assuming that the Navigation-Properties are named like the Sets... 您可以尝试以下操作:假设导航属性的名称类似于Sets ...

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);
 }

Edit: 编辑:

Change your Patient-class like this, then your problem should be gone: 像这样更改您的患者级别,然后您的问题应该消失了:

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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM