平面数据的层次结构
我有一个雇员类,它有一个employeeId (int)、parent(int) 和children 属性List
。我以正确的顺序从数据库中获取员工列表,现在需要构建层次结构,但我失败得很惨...我知道这是编程 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以首先创建所有员工对象的列表并设置 EmployeeId 和 ParentId 属性。如果您还将它们放入字典中,并以
EmployeeId
为键,则可以随后检索每个的父级以添加到Children
集合中:You can start by creating a list of all the employee objects and setting the
EmployeeId
andParentId
properties. If you also put them in a dictionary, keyed byEmployeeId
, you can retrieve the parent of each afterward to add to theChildren
collection:不久前我从这篇文章中得到了灵感(我不得不稍微改变它以适应我的目的)。它基本上构建了一个n级的层次结构。
可能有用,即使只是在您自己的情况下打折它的方法:-)
http://www.scip.be/index.php?Page=ArticlesNET23&Lang=EN
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