I have two lists, one's for employees and the other is for department. I NEED TO print the employee list under manager. This is my code.
public class Model { public static List<Employee> employees = new List<Employee> { new Employee { empID = "201", empName = "Afreen", DepartmentId = 201, designation = "Intern"}, new Employee { empID = "202", empName = "Resmi", DepartmentId = 201, designation = "Software Developer" }, new Employee { empID = "203", empName = "Najat", DepartmentId = 202, designation = "E- commerce representative" }, new Employee { empID = "204", empName = "Bincy", DepartmentId = 203, designation = "buying team" }, new Employee { empID = "205", empName = "Sara", DepartmentId = 204, designation = "Marketing associate" }, new Employee { empID = "206", empName = "Kashif", DepartmentId = 201, designation = "Automative Testing" }, new Employee { empID = "207", empName = "Noor", DepartmentId = 205, designation = "HR representative" }, new Employee { empID = "208", empName = "Saad", DepartmentId = 201, designation = "Web developer" }, new Employee { empID = "209", empName = "Kylie", DepartmentId = 205, designation = "HR representative" }, new Employee { empID = "210", empName = "Kendall", DepartmentId = 203, designation = "buying team" } }; public static List<Department> departments = new List<Department>() { new Department {DepartmentId=201, departmentname="IT", manager= "York" }, new Department {DepartmentId=202, departmentname="IT", manager= "York" }, new Department {DepartmentId=203, departmentname="Buying", manager= "Mohammad" }, new Department {DepartmentId=204, departmentname="Marketting", manager= "Chang" }, new Department {DepartmentId=205, departmentname="HR", manager= "Ameera"}, }; } } } public void deppp() { bool incorrect = true; Console.WriteLine("(get employees) Enter the manager's name?"); var managename = Console.ReadLine(); foreach (var deps in Model.departments) { foreach (var emps in Model.employees.Where(y => y.DepartmentId == deps.DepartmentId)) { while (incorrect) { if (string.IsNullOrEmpty(managename)) { Console.WriteLine("Manager name cant be empty, enter name again"); managename = Console.ReadLine(); } else if (!string.IsNullOrEmpty(managename) && !Model.departments.Any(x => x.manager.ToLower().Trim().Equals(managename.ToLower()))) { Console.WriteLine("Manager name invalid, enter name again"); managename = Console.ReadLine(); } //if (ManagerID == 0) //{ // Console.WriteLine("Manager ID cant be empty, enter name again"); //} else { var ManagerID = Model.departments.Where(x => x.manager.ToLower().Equals(managename.ToLower())).Select(x => x.DepartmentId).FirstOrDefault(); var EmployeesUnderMan = Model.employees.Where(x => x.DepartmentId == ManagerID).Select(x => x.empName).ToList(); Console.WriteLine((string.Join(" ", EmployeesUnderMan))); incorrect = false; } } } } }
Now how do i print a list of employees. where Managers are INCLUDED as employees,