0

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

我有一个包含一些自引用项目的列表,但是如果它们基于它们的键属于一起,我如何将它们放入列表列表中。

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

这是一些代码。

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

    }
4

3 回答 3

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 列表。现在填充 call 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));
        }

    }
于 2012-12-17T19:58:33.030 回答
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 = "" });

这允许您仅填充单个员工列表,并且您可以使用单个员工对象上的属性从那里获取每个员工的老板或他们的下属。

于 2012-12-17T20:20:23.023 回答
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;
        }
于 2012-12-17T20:13:54.800 回答