我无法让我的递归再次工作:/
我有一个包含一些自引用项目的列表,但是如果它们基于它们的键属于一起,我如何将它们放入列表列表中。
有人可以帮我解决这个问题吗?请 :)
这是一些代码。
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);
}
}