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

C# 自引用列表到多个结构化列表?,c#,list,recursion,self-reference,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

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

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

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

下面是一些代码

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

    }
公共类员工
{
public int employeeID{get;set;}
public int?parentEmployeeID{get;set;}
公共字符串名称{get;set;}
公共字符串位置{get;set;}
}
List Employeelist=新列表{
新员工{employeeID=1,parentEmployeeID=null,Name=“Mike”,Position=“CIO”},
新员工{employeeID=2,parentEmployeeID=1,Name=“Robs”,Position=“Sales”},
新员工{employeeID=3,parentEmployeeID=7,Name=“Fred”,Position=“Manager”},
新员工{employeeID=4,parentEmployeeID=6,Name=“Pablo”,Position=“Economy”},
新员工{employeeID=5,parentEmployeeID=2,Name=“Erica”,Position=“Sometingelse”},
新员工{employeeID=6,parentEmployeeID=null,Name=“Obama”,Position=“”},
新员工{employeeID=7,parentEmployeeID=5,Name=“Brad”,Position=“”},
新员工{employeeID=8,parentEmployeeID=3,Name=“艾米”,Position=“”},
新员工{employeeID=9,parentEmployeeID=4,Name=“Howard”,Position=“”},
};
List StrucutedEmployeeList=新列表();
私有void ArrangeInNewlistofLists(员工根目录,int?parentOptionID)
{
foreach(Employeelist.Where中的Employees选项(x=>x.employeeID==parentOptionID))
{
列表温度=新列表();
structedemployeelist.Add(临时);
安排新的列表列表(选项,选项.parentEmployeeID);
}
}
public void ArrangeListWithRecursion()
{
foreach(员工列表中的变量项)
{
if(item.parentEmployeeID==null)
ArrangeInNewListFlists(项,空);
}
}
首先:
foreach(Employeelist.Where(x=>x.employeeID==parentOptionID)中的Employees选项)
-这将永远不会返回任何结果,因为您没有ID为null的员工

我想你想要的是x.parentEmployeeID

foreach (Employees option in Employeelist.Where(x => x.parentEmployeeID == parentOptionID))
此外,这也不重要,因为:

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

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

        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;
        }
公共类员工
{
public int employeeID{get;set;}
public int?parentEmployeeID{get;set;}
公共字符串名称{get;set;}
公共字符串位置{get;set;}
公共列表子项{get;set;}
}
公众假期安排
{
Employeelist=ArrangeListWithRecursion();
}
私有列表ArrangeListWithRecursion(int?parentId=null)
{
var result=新列表();
foreach(Employeelist.Where(e=>e.parentEmployeeID==parentId))中的var employee)
{
var children=Employeelist.Where(e=>e.parentEmployeeID==employee.employeeID.ToList();
employee.Children=带有递归的排列列表(employee.employeeID);
结果。添加(员工);
}
返回结果;
}

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

雇员类别:

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

这允许您仅填充一个员工列表,从中可以使用单个员工对象上的属性获取每个员工的上司或下属。

我已编辑了您的标题。请看,“,其中的共识是“不,他们不应该”。我真的可以改变我雇佣员工的班级结构,但我想计算一下迈克手下有多少孩子是空的。这可能吗?我对你的问题感到困惑。为了使员工成为mike的子女,他们必须将其parentEmployeeID设置为mike的employeeID且不为空。
        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;
        }
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 = "" });