平面数据的层次结构

发布于 2024-10-15 04:20:58 字数 457 浏览 2 评论 0原文

我有一个雇员类,它有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜是你 2024-10-22 04:20:58
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();
}
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();
}
給妳壹絲溫柔 2024-10-22 04:20:58

您可以首先创建所有员工对象的列表并设置 EmployeeId 和 ParentId 属性。如果您还将它们放入字典中,并以 EmployeeId 为键,则可以随后检索每个的父级以添加到 Children 集合中:

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);
}

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);
}
零度° 2024-10-22 04:20:58

不久前我从这篇文章中得到了灵感(我不得不稍微改变它以适应我的目的)。它基本上构建了一个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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文