public IEnumerable <ReportBO> GetAllPatientsReports_DAL() { try { using (HospitalManagementSystemDataContext objHmsDataContext = new HospitalManagementSystemDataContext(Utils.ConnectionString)) { if (objHmsDataContext.Connection.State == System.Data.ConnectionState.Closed) { objHmsDataContext.Connection.Open(); } var Reports = objHmsDataContext.Reports .Select(patientReports => new ReportBO { report_ID = patientReports.Report_ID, patient_ID = patientReports.Patient_ID, labResult = patientReports.Lab_Result, reportTime = Convert.ToString(patientReports.Report_Time), date = patientReports.Report_Date }).GroupBy(p => p.patient_ID).First(); foreach (var rr in Reports) { rr.reportRequested = objHmsDataContext.Appointments.Where(a => a.Appointment_ID == rr.appointment_ID).Select(ap => ap.RequestedReportNotes).FirstOrDefault(); } var r = objHmsDataContext.ExecuteCommand("select Report_ID, Patient_ID,LabIncharge_ID,Lab_Result,Report_Time,Report_Date from ( select *, row_Number() over(partition by Patient_ID order by Report_ID) as rnum from Report) source where rnum = 1; "); return(Reports); } } catch (Exception ex) { IEnumerable <ReportBO> report_BO = null; return(report_BO); } }