自引用列表到多个结构化列表?

时间:2012-12-17 19:43:16

标签: c# list recursion self-reference

我无法让我的递归再次起作用:/

我有一个包含一些自引用项目的列表,但如果它们根据键分别列在一起,我怎么能把它们放在列表中。

有人可以帮我解决这个问题吗?请:)

这是一些代码。

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);
        }

    }

3 个答案:

答案 0 :(得分:0)

首先: foreach (Employees option in Employeelist.Where(x => x.employeeID == parentOptionID)) - 这将永远不会返回任何结果,因为您没有ID为空的员工......

我认为你想要x.parentEmployeeID例如

foreach (Employees option in Employeelist.Where(x => x.parentEmployeeID == parentOptionID))

此外,这不是因为:

List<Employees> temp = new List<Employees>();
StrucutedEmployeeList.Add(temp);

你总是添加空列表,而不是用它们做任何其他事情......

这应该做你想要的:

public class Employees
{
    public int employeeID { get; set; }
    public int? parentEmployeeID { get; set; }
    public string Name { get; set; }
    public string Position { get; set; }

    public List<Employees> subEmp { get; set; }
}

注意你有subEmp列表。 现在填充调用ArrangeListWithRecursion()

    List<Employees> StrucutedEmployeeList = new List<Employees>();
    private Employees ArrangeInNewlistofLists(Employees item, int? parentOptionID)
    {
        item.subEmp = new List<Employees>();

        foreach (Employees option in Employeelist.Where(x => x.parentEmployeeID == parentOptionID))
        {
            item.subEmp.Add(ArrangeInNewlistofLists(option, item.employeeID));
        }
        return item;
    }

    public void ArrangeListWithRecursion()
    {
        foreach (var item in Employeelist.Where(x=>x.parentEmployeeID == null))
        {
            StrucutedEmployeeList.Add(ArrangeInNewlistofLists(item, item.employeeID));
        }

    }

答案 1 :(得分:0)

构建代码的方式不允许真正的递归解决方案。通过将children属性添加到Employees,您将获得所需的解决方案。

        public class Employees
        {
            public int employeeID { get; set; }
            public int? parentEmployeeID { get; set; }
            public string Name { get; set; }
            public string Position { get; set; }

            public List<Employees> Children { get; set; }
        }


        public void Arrange()
        {
            Employeelist = ArrangeListWithRecursion();
        }

        private List<Employees> ArrangeListWithRecursion(int? parentId = null)
        {
            var result = new List<Employees>();
            foreach (var employee in Employeelist.Where(e => e.parentEmployeeID == parentId))
            {
                var children = Employeelist.Where(e => e.parentEmployeeID == employee.employeeID).ToList();
                employee.Children = ArrangeListWithRecursion(employee.employeeID);
                result.Add(employee);
            }
            return result;
        }

答案 2 :(得分:0)

我不太确定你想用你的例子来完成什么。假设您正在尝试将相关员工组织在一起,一种方法可能是重新组织您的对象,如下所示:

员工类:

public class Employees : List<Employee>
{
    public new void Add(Employee employee)
    {
        employee.employees = this;
        base.Add(employee);
    }
}

员工类:

public class Employee
{
    public Employees employees { get; set; }
    public int employeeID { get; set; }
    public int? parentEmployeeID { get; set; }
    public string Name { get; set; }
    public string Position { get; set; }

    public Employee Boss 
    {
        get 
        {
            return employees.FirstOrDefault(e => e.employeeID == this.parentEmployeeID); 
        }
    }

    public IEnumerable<Employee> Subordinates 
    { 
        get
        {
            return employees.Where(e => e.parentEmployeeID == this.employeeID);
        }
    }
}

填充员工:

var employees = new Employees();
employees.Add(new Employee { employeeID = 1, parentEmployeeID = null, Name = "Mike", Position = "CIO" });
employees.Add(new Employee { employeeID = 2, parentEmployeeID = 1, Name = "Robs", Position = "Sales" });
employees.Add(new Employee { employeeID = 3, parentEmployeeID = 7, Name = "Fred", Position = "Manager" });
employees.Add(new Employee { employeeID = 4, parentEmployeeID = 6, Name = "Pablo", Position = "Economy" });
employees.Add(new Employee { employeeID = 5, parentEmployeeID = 2, Name = "Erica", Position = "Sometingelse" });
employees.Add(new Employee { employeeID = 6, parentEmployeeID = null, Name = "Obama", Position = "" });
employees.Add(new Employee { employeeID = 7, parentEmployeeID = 5, Name = "Brad", Position = "" });
employees.Add(new Employee { employeeID = 8, parentEmployeeID = 2, Name = "Amy", Position = "" });
employees.Add(new Employee { employeeID = 9, parentEmployeeID = 2, Name = "Howard", Position = "" });

这允许您只填充一个员工列表,然后您可以使用单个Employee对象上的属性获取每个员工的Boss或其下属。