【发布时间】:2011-01-31 19:52:36
【问题描述】:
我有一个员工类,它有一个employeeId (int)、parent(int) 和children 属性List<Employee>。我以正确的顺序从数据库中获取员工列表现在需要构建层次结构,但我失败得很惨……我知道这是编程 101,但我很难它。
public class Employee
{
public int EmployeeId { get; set;}
public int ParentId;{ get; set;}
public List<Employee> Children; { get; set;}
}
数据示例
EmployeeId, ManagerId
1, 0 //no one
2, 1
3, 1
4, 2
5, 2
6, 3
7, 3
【问题讨论】:
-
您尝试过什么来构建层次结构?你能发布你的代码和你遇到的问题吗?
-
你有什么问题?员工的孩子跟什么有什么关系?
-
@Qwertie - 他想知道如何在 C# 中填充正确的层次结构,因为它存储在数据库中。
-
@Oded,我尝试跟踪 parentId 并在父 id 与当前项目的父 id 相同时添加子项,但在进入 2+ 级时失败。
-
@Qwertie,我把问题改成了粗体。