public static List <ChuyenKhoaDTO> GetJoin() { var data = new SchedureEntities().SP_ChuyenKhoa_GetAll().ToList(); List <ChuyenKhoaDTO> lst = new List <ChuyenKhoaDTO>(); foreach (var gr in data.GroupBy(q => q.IDChuyenKhoa)) { var first = gr.First(); ChuyenKhoaDTO chuyenKhoa = new ChuyenKhoaDTO() { IDChuyenKhoa = first.IDChuyenKhoa, Avatar = first.Avatar, Name = first.ChuyenKhoa_Name, PhongBans = new List <PhongBanDTO>(), Status = first.ChuyenKhoa_Status, TimeUse = first.TimeUse, }; foreach (var pb in gr.GroupBy(q => q.PhongBan_Id)) { var pb_first = pb.First(); var phongban = new PhongBanDTO() { IDChuyenKhoa = pb_first.IDChuyenKhoa, IDPhongBan = pb_first.PhongBan_Id, TenPhongBan = pb_first.TenPhongBan, PhongBan_Id = pb_first.PhongBan_Id, Doctors = new List <DoctorDTO>(), Status = "ACTIVE", }; foreach (var bs in pb.GroupBy(q => q.NhanVien_Id)) { var bs_first = bs.First(); var bacsi = new DoctorDTO { IDDoctor = bs_first.NhanVien_Id, FullName = bs_first.TenNhanVien, LichLamViecs = new HashSet <LichLamViecDTO>(), }; foreach (var lich in bs) { LichLamViecDTO lichLamViec = new LichLamViecDTO { IDLich = lich.IDLich, Status = lich.LichLamViec_Status, Date = lich.Date, TimeSlot = new TimeSlotDTO { HourEnd = lich.HourEnd, HourStart = lich.HourStart, Name = lich.TimeSplot_Name, Status = lich.TimeSlot_Status, }, Doctor = new DoctorDTO { IDDoctor = bs_first.NhanVien_Id, FullName = bs_first.TenNhanVien, LichLamViecs = new HashSet <LichLamViecDTO>(), } }; bacsi.LichLamViecs.Add(lichLamViec); } phongban.Doctors.Add(bacsi); } chuyenKhoa.PhongBans.Add(phongban); } lst.Add(chuyenKhoa); } return(lst.OrderBy(q => q.Name).ToList()); }