【问题标题】:Self Referenced list to multiple structured lists?自引用列表到多个结构化列表?
【发布时间】:2012-12-17 19:43:16
【问题描述】:

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

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

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

这是一些代码。

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

    }

【问题讨论】:

标签: c# list recursion self-reference


【解决方案1】:

您构建代码的方式不允许真正的递归解决方案。通过将 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】:

    我不太确定您要通过示例完成什么。假设您尝试将相关员工分组在一起,一种方法可能是像这样重新组织您的对象:

    员工类:

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

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

    【讨论】:

    • 我真的可以改变我得到员工的班级的结构,但是假设我想计算 Mike 下有多少孩子有 null 有可能吗?
    • 我对你的问题感到困惑。为了让员工成为 Mike 的孩子,他们必须将其 parentEmployeeID 设置为 Mike 的员工 ID,而不是 null。
    【解决方案3】:

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

    我想你想要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));
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多