再帰を再び機能させることができません:/
いくつかの自己参照項目を含むリストがありますが、それらがキーに基づいて一緒に属している場合、それらをリストのリストに入れるにはどうすればよいですか。
誰かがこの問題で私を助けることができますか? お願いします :)
ここにいくつかのコードがあります。
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);
}
}