private void UpdateInstructorCourses(string[] selectedCourses, IEnumerable <Course> courses) { if (selectedCourses == null) { CourseAssignments = new List <CourseAssignment>(); return; } var selectedCoursesHS = new HashSet <string>(selectedCourses); var instructorCourses = new HashSet <int> (CourseAssignments.Select(c => c.CourseID)); foreach (var course in courses) { if (selectedCoursesHS.Contains(course.Id.ToString())) { if (!instructorCourses.Contains(course.Id)) { CourseAssignments.Add(new CourseAssignment { Course = course, Instructor = this }); } } else { if (instructorCourses.Contains(course.Id)) { var toRemove = CourseAssignments.Single(ci => ci.CourseID == course.Id); CourseAssignments.Remove(toRemove); } } } }
private void UpdateInstructorCourses(IEnumerable <CreateEdit.Command.CourseData> courses) { var assignedCourseIds = courses .Where(x => x.Assigned).Select(x => x.Id) .ToArray(); var removedCourses = CourseAssignments .Where(x => !assignedCourseIds.Contains(x.CourseId)) .ToArray(); var previouslyAssignedCourseIds = CourseAssignments .Select(x => x.CourseId) .ToArray(); var addedCourses = assignedCourseIds .Where(x => !previouslyAssignedCourseIds.Contains(x)) .Select(x => new CourseAssignment { CourseId = x, InstructorId = Id }) .ToArray(); foreach (var removedCourse in removedCourses) { CourseAssignments.Remove(removedCourse); } foreach (var addedCourse in addedCourses) { CourseAssignments.Add(addedCourse); } }