我不能让我的递归再次工作:/自引用列表到多个结构化列表?
我有一个列表,其中包含一些自我指涉的项目,但如何将它们放在列表中,如果他们属于一起基于他们的密钥。
有人可以帮我解决这个问题吗?请:)
这是一些代码。
public class Employees
{
public int employeeID { get; set; }
public int? parentEmployeeID { get; set; }
public string Name { get; set; }
public string Position { get; set; }
}
List<Employees> Employeelist = new List<Employees> {
new Employees { employeeID = 1, parentEmployeeID = null, Name = "Mike", Position = "CIO" },
new Employees { employeeID = 2, parentEmployeeID = 1, Name = "Robs", Position = "Sales" },
new Employees { employeeID = 3, parentEmployeeID = 7, Name = "Fred", Position = "Manager" },
new Employees { employeeID = 4, parentEmployeeID = 6, Name = "Pablo", Position = "Economy" },
new Employees { employeeID = 5, parentEmployeeID = 2, Name = "Erica", Position = "Sometingelse" },
new Employees { employeeID = 6, parentEmployeeID = null, Name = "Obama", Position = "" },
new Employees { employeeID = 7, parentEmployeeID = 5, Name = "Brad", Position = "" },
new Employees { employeeID = 8, parentEmployeeID = 3, Name = "Amy", Position = "" },
new Employees { employeeID = 9, parentEmployeeID = 4, Name = "Howard", Position = "" },
};
List<List<Employees>> StrucutedEmployeeList = new List<List<Employees>>();
private void ArrangeInNewlistofLists(Employees root, int? parentOptionID)
{
foreach (Employees option in Employeelist.Where(x => x.employeeID == parentOptionID))
{
List<Employees> temp = new List<Employees>();
StrucutedEmployeeList.Add(temp);
ArrangeInNewlistofLists(option, option.parentEmployeeID);
}
}
public void ArrangeListWithRecursion()
{
foreach (var item in Employeelist)
{
if (item.parentEmployeeID == null)
ArrangeInNewlistofLists(item, null);
}
}
我编辑了你的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 –