/// <summary> /// Method to REMOVE Department and MOVE all it's Employees to another /// </summary> /// <param name="departmentToRemove">Department to REMOVE</param> /// <param name="departmentToAddEmployees">Department in witch MOVE</param> public void RemoveDepartment(Department departmentToRemove, Department departmentToAddEmployees) { Department parent = DepartmentsDb.Find(x => x.DepartmentName == departmentToRemove.ParentDepartment); if (parent != null) { parent.innerDepartments.Remove(departmentToRemove); } else { company.Clear(); } foreach (Employee e in EmployeesDB) { if (e.Department == departmentToRemove.DepartmentName) { e.Department = departmentToAddEmployees.DepartmentName; } } MoveChildren(departmentToRemove, departmentToAddEmployees); RemoveChildren(departmentToRemove); DepartmentsDb.Remove(departmentToRemove); }
/// <summary> /// Method to REMOVE Department /// </summary> /// <param name="department">Department to Remove</param> public void RemoveDepartment(Department department) { Department parent = DepartmentsDb.Find(x => x.DepartmentName == department.ParentDepartment); if (parent != null) { parent.innerDepartments.Remove(department); } else { company.Clear(); } if (department.DepartmentName != DepartmentsDb.Find(x => x is Company).DepartmentName) { RemoveChildren(department); DepartmentsDb.Remove(department); } else { DepartmentsDb.Clear(); } EmployeesDB = EmployeesDB.Where(x => x.Department != department.DepartmentName).ToList(); }
/// <summary> /// Method to REMOVE Children from removed Department /// </summary> /// <param name="parent">Department to Remove</param> void RemoveChildren(Department parent) { for (int i = 0; i < DepartmentsDb.Count; i++) { if (DepartmentsDb[i].ParentDepartment == parent.DepartmentName) { RemoveChildren(DepartmentsDb[i]); DepartmentsDb.Remove(DepartmentsDb[i]); } } }