我不能讓我的遞歸再次工作:/自引用列表到多個結構化列表?
我有一個列表,其中包含一些自我指涉的項目,但如何將它們放在列表中,如果他們屬於一起基於他們的密鑰。
有人可以幫我解決這個問題嗎?請:)
這是一些代碼。
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/)」,其中的共識是「不,他們不應該」。 –