1

I have an employee class that has an employeeId (int), parent(int) and children property List<Employee>. I get the employee list from the database in the correct order and now need to build the hierarchy, but I am failing miserably...I know this is programming 101, but I am having a hard time with it.

public class Employee
{
  public int EmployeeId { get; set;}
  public int ParentId;{ get; set;}
  public List<Employee> Children; { get; set;}

}

Data Example

EmployeeId, ManagerId
1, 0 //no one
2, 1
3, 1
4, 2
5, 2
6, 3
7, 3
John Saunders
  • 160,644
  • 26
  • 247
  • 397
12354
  • 13
  • 1
  • 4
  • What have you tried in order to build the hierarchy? Can you post your code and the issues you are having? – Oded Jan 31 '11 at 19:55
  • What's your question? What do the employee's Children have to do with anything? – Qwertie Jan 31 '11 at 19:56
  • @Qwertie - he wants to know how to populate the correct hierarchy in C# as it is stored in the database. – Oded Jan 31 '11 at 19:58
  • @Oded, I tried keeping track of the parentId and adding Children if the parent id is the same as the current item's parent id, but then failed when going to level 2+. – 12354 Jan 31 '11 at 19:59
  • @Qwertie, i changed question to bold. – 12354 Jan 31 '11 at 19:59
  • @12345 - did you sort the records by `ManagerId`? – Oded Jan 31 '11 at 19:59
  • @Oded - Yes, the result is sorted. I've done this before, just can't find the src. I am aware there may be some recursion involved (maybe not since it is sorted...) but i've spent a while trying to figure this out...frustrating. – 12354 Jan 31 '11 at 20:01

3 Answers3

2
List<Employee> allEmployees = new List<Employee>();
allEmployees.AddRange(LoadAllEmployees()); // pull from DB in flat format    
foreach (var employee in allEmployees)
{
  employee.Children = allEmployees.Where(e => e.ParentId == employee.EmployeeId).ToList();
}
jbehren
  • 780
  • 6
  • 11
1

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<Employee> employees = new List<Employee>();
Dictionary<int,Employee> dict = new Dictionary<int,Employee>();

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);
}
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
  • the only problem is my dictionary ends up having all the items PLUS the hierarchical data. So if my list from the db contains 13 items, I end up with 13 items and then some of the items have children...instead of say 4 items with 9 children. Does that make sense? – 12354 Jan 31 '11 at 20:41
  • The dictionary is only needed for looking up the ParentId to add to the Children collection. Once the hieracrhy is constructed, you can forget about the dictionary altogether. If you just want the roots of the hierarchies then just keep a reference to each of the top-level employees and you can access the rest via the Children collections. – Mark Cidade Jan 31 '11 at 20:51
  • That makes sense, but how do I know which are the top level parents in hierarchy? – 12354 Jan 31 '11 at 21:05
  • If all top-level employees have a ParentId of zero, then select those when iterating through the employees list, e.g., `if (e.ParentId==0) topLevelEmployees.Add(e);` – Mark Cidade Jan 31 '11 at 21:08
1

i got inspiration from this article a while ago (i had to change it slightly to suit my purposes). It basically builds a hierarchical structure to the n'th degree.

Might be useful, even if only to discount its approach in your own case :-)

http://www.scip.be/index.php?Page=ArticlesNET23&Lang=EN

jim tollan
  • 22,305
  • 4
  • 49
  • 63