I have an employee class that has an employeeId (int), parent(int) and children property List
. I get the employee list from the database in the
You can start by creating a list of all the employee objects and setting the EmployeeId
and ParentId
properties. If you also put them in a dictionary, keyed by EmployeeId
, you can retrieve the parent of each afterward to add to the Children
collection:
List employees = new List();
Dictionary dict = new Dictionary();
foreach(result from database query)
{
Employee employee = new Employee();
employee.EmployeeId = result["EmployeeId"];
employee.ParentId = result["ParentId"];
employees.Add(employee);
dict.Add(employee.EmployeeId, employee);
}
foreach(Employee e in employees)
{
dict[e.ParentId].Children.Add(e);
}