public JsonResult GetAllocatedClassroomsByDepartment(int departmentId) { //all the isAllocated classroom for this department var classroom = _classRoomManager.GetAllocatedClassroomsByDepartment(departmentId); //As class room allocation is having all the course id we are selecting the distinct course id from them var allocatedCourse = classroom.Select(c => c.Course).Distinct().Select(a => new { CourseId = a.Id // if i select (c => c.Course.Id) anonymous object creation not possible }).ToList(); //Bringing all the courses by department id var courses = _courseManager.GetAllCourses().Where(c => c.DepartmentId == departmentId).ToList(); //After bringing all the course id only joining the distinct courseId we have from class room allocation tbl var allocatedClassroomInfo = allocatedCourse.Join(courses, a => a.CourseId, c => c.Id, (ac, cls) => new { CourseId = ac.CourseId, CourseName = cls.CourseName, CourseCode = cls.CourseCode, ScheduleInfo = ScheduleInfo(departmentId, ac.CourseId).ToString()//sending department id, and distinct course id for schedule info }).ToList(); //From that department's all the courses we are selecting rest of the courses id except the id's from clsrm alctn tbl var unallocatedCourses = courses.Select(c => new { CourseId = c.Id }).Except(allocatedClassroomInfo.Select(c => new { CourseId = c.CourseId })).Select(c => new { CourseId = c.CourseId }); //Joining those id with the course tbl, and setting the schedule info as not scheduled var unallocatedCoursesInfo = unallocatedCourses.Join(courses, u => u.CourseId, c => c.Id, (u, ua) => new { CourseId = ua.Id, CourseName = ua.CourseName, CourseCode = ua.CourseCode, ScheduleInfo = "Not Scheduled Yet" }); // Union between alctd classroom courseId and all the other course id and their properties var allocatedClassroom = unallocatedCoursesInfo.Union(allocatedClassroomInfo).OrderBy(c => c.CourseId).ToList(); return(Json(allocatedClassroom)); }