/// <summary> /// Commits the courses, that were moved to the /// pre-reqs listview, to the preReqs table /// </summary> public void CommitPreReqs() { try { using (var context = new SchoolU_DBEntities()) { // Gets all the courses names IList <string> allCourseName = context.Courses.Where(i => i.CourseId != 0).Select(i => i.CourseName).ToList(); // Gets the course id for the course that was recently added int courseAdded = context.Courses.Where(i => i.CourseName == CourseName).Select(i => i.CourseId).Single(); if (SelectedCourseCollection.Any()) { // Stores the PreRequisite objects so they can be added all at the same time using the AddRange method IList <PreRequisite> PreReqList = new List <PreRequisite>(); if (IsPreReqListDifferent()) { for (int i = 0; i < GetPreReqIds().Count; i++) { var newPreReq = new PreRequisite() { CourseId = courseAdded, PrereqId = GetPreReqIds().ElementAt(i) }; PreReqList.Add(newPreReq); } context.PreRequisites.AddRange(PreReqList); } context.SaveChanges(); } } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
/// <summary> /// Gets a list of course Ids based on the pre-req names /// currently in the pre-reqs list (SelectedCourseCollection) /// </summary> /// <returns></returns> public IList <int> GetPreReqIds() { IList <int> preReqIds = new List <int>(); try { using (var context = new SchoolU_DBEntities()) { if (SelectedCourseCollection.Any()) { // Gets all the pre-req courses names IList <string> pre_reqNames = SelectedCourseCollection.Where(i => i.CourseName != string.Empty).Select(i => i.CourseName).ToList(); // Gets all the course ids off of the courses names added to the pre-reqs list // pre-req = course ids return(preReqIds = context.Courses.Where(i => pre_reqNames.Contains(i.CourseName)).Select(i => i.CourseId).ToList()); } } } catch (Exception ex) { MessageBox.Show(ex.Message); return(null); } return(null); }