재귀가 다시 작동하지 않습니다./다중 참조 목록에 자체 참조 된 목록?
자체 참조 항목이 포함 된 목록을 가지고 있지만 키를 기반으로 함께 있으면 목록에 넣을 수 있습니다.
누군가가이 문제에 대해 도움을 줄 수 있습니까? 제발 :)
여기에 몇 가지 코드가 있습니다. 모든
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/)"합의가 "아니오, 그렇지 않아야합니다"로 표시되어야합니다. –