private void ChangeAndAddActivity(string changeoradd) { //NEW GUIDES Activity_Service activity_Service = new Activity_Service(); List <Guide> guideNew = new List <Guide>(); List <string> guideNamesNew = new List <string>(); if (cbSelectGuideName1.Text != string.Empty) { Guide guide = new Guide() { GuideName = cbSelectGuideName1.Text, GuideNumber = int.Parse(lblGuideIdOutput1.Text), }; guideNew.Add(guide); } if (cbSelectGuideName2.Text != string.Empty) { Guide guide = new Guide() { GuideName = cbSelectGuideName2.Text, GuideNumber = int.Parse(lblGuideIdOutput2.Text), }; guideNew.Add(guide); } if (cbSelectGuideName3.Text != string.Empty) { Guide guide = new Guide() { GuideName = cbSelectGuideName3.Text, GuideNumber = int.Parse(lblGuideIdOutput3.Text), }; guideNew.Add(guide); } if (cbSelectGuideName4.Text != string.Empty) { Guide guide = new Guide() { GuideName = cbSelectGuideName4.Text, GuideNumber = int.Parse(lblGuideIdOutput4.Text), }; guideNew.Add(guide); } foreach (Guide guideNameNew in guideNew) { guideNamesNew.Add(guideNameNew.GuideName); } bool isUnique = guideNamesNew.Distinct().Count() == guideNamesNew.Count(); if (isUnique == false) { throw new Exception($"Not all guidenames are unique "); } TimeSpan startTime = dtpActivityStartDate.Value.TimeOfDay; TimeSpan endTime = dtpActivityEndDate.Value.TimeOfDay; DateTime date = dtpActivityDate.Value.Date; string activityName = txtActivityName.Text; int numberOfStudents = int.Parse(txtNumberOfStudents.Text); List <Guide> guideNew2 = new List <Guide>(guideNew); List <Guide> guideNew4 = new List <Guide>(guideNew); string output = ""; // check of de naam al wordt gebruikt activityList = activity_Service.GetActivities(); foreach (Activity activityL in activityList) { if (activityL.ActivityName == activityName) { string exc = $"There is already an activity with name {activityName}"; lblModifyActivityLogOutput.Text = exc; throw new Exception($"There is already an activity with name {activityName}"); } } if (changeoradd == "change") { guides = GetAllGuidesById(int.Parse(lblActivityIdOutput.Text)); string activityNameOld = cbSelectActivity.Text; int activityId = int.Parse(lblActivityIdOutput.Text); //CHANGE ACTIVITY //CHECK OLD WANS for (int i = 0; i < guides.Count; i++) { //als de nieuwe lijst een persoon uit de oude lijst bevat: schrijf de nieuwe info over de oude persoon heen //if (guideNew.Contains(guides[i])) if (guideNew.Any(guiden => guiden.GuideNumber == guides[i].GuideNumber)) { activity_Service.ChangeSchedule(activityId, guides[i].GuideNumber, date, startTime, endTime); //Verwijder uit de nieuwe begeleiderlijst de nog te doorlopen begeleider, je hebt hem hier immers al gecheckt. //CHECK WHO THIS IS AND REMOVE FROM NEW Guide guideSelect = guides[i]; List <Guide> guideNew3 = new List <Guide>(guideNew2); foreach (Guide guideN in guideNew3) { if (guideN.GuideNumber == guideSelect.GuideNumber) { guideNew2.Remove(guideN); } } } //als de nieuwe lijst de persoon uit de oude lijst niet bevat: verwijder de persoon uit de oude lijst else { activity_Service.DeleteFromSchedule(guides[i].GuideNumber); } } for (int i = 0; i < guideNew2.Count; i++) { //Hier zitten nu alleen unieke nieuwe waarden in. activity_Service.AddToSchedule(activityId, guideNew2[i].GuideNumber, date, startTime, endTime); } if (guideNew4.Count == 0) { try { activity_Service.DeleteFromSchedule(0); } catch { }; activity_Service.AddToSchedule(activityId, 0, date, startTime, endTime); } activity_Service.ChangeActivity(activityName, numberOfStudents, guideNamesNew.Count, activityNameOld); output = $"{activityName} has been changed in the database "; ResetModifyActivity(); lblModifyActivityLogOutput.Text = output; } else if (changeoradd == "add") { //ADD ACTIVITY activity_Service.AddToActivities(activityName, numberOfStudents, guideNamesNew.Count); //ADD GUIDES Activity activity = activity_Service.GetActivityByName(activityName); if (guideNew2.Count == 0) { for (int i = 0; i < guideNew2.Count; i++) { activity_Service.AddToSchedule(activity.ActivityNumber, guideNew2[i].GuideNumber, date, startTime, endTime); } } else { activity_Service.AddToSchedule(activity.ActivityNumber, 0, date, startTime, endTime); } lblModifyActivityLogOutput.Text = $"{activityName} has been changed in the database "; output = $"{activityName} has been added in the database "; } ResetModifyActivity(); lblModifyActivityLogOutput.Text = output; }