简体   繁体   中英

How to retrieve data from multiple tables using EF 5 linq method

I am facing a problem as discussed below.

I have four tables

  • PateintTable
  • PatientRecordTable(FK=PatientID)
  • RecordMedicineTable(FK=MedicineID,RecordID)
  • MedicineTable

I want to get a specific Patient data depending upon the PatientID of the patient along with the patient data, I want to obtain his medical-records (PatientRecordTable) plus medicines associated with each of his records.

I tried many tricks but exceptions occur when I try to get the medicines.

 lstPrescribedMedicines.Items.Clear();
     foreach (var item in result.RecordsMedicines)
     {
          lstPrescribedMedicines.Items.Add(item.Medicine.MedName);
     }

I am implementing using(context=new mss-context()) . This is my query:

var pateintRecords = context
          .PateintRecords
          .Include(p => p.Patient)
          .Include(m=>m.RecordsMedicines)
          .Where(x => x.PatientID == pateintID).ToList();// as IQueryable;

在此处输入图像描述

When working with children of children, you have to include them:

var pateintRecords = context
          .PateintRecords
          .Include(p => p.Patient)
          .Include(m=>m.RecordsMedicines)
          .Include(m=>m.RecordsMedicines.Select(i => i.Items))
          .Where(x => x.PatientID == pateintID).ToList();// as IQueryable;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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