2012-12-17 45 views
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); 
     } 

    } 
+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

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

    } 
0

你正在構建的代碼不會讓一個真正的遞歸解決方案的方式。通過將子項屬性添加到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; 
     } 
0

我不太清楚你在用你的例子來完成什麼。假設你正在試圖將你的相關員工一起,一個辦法可以重新組織你的對象像這樣:

Employees類:

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

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

這允許您只填充單個li並從那裏你可以使用單個Employee對象上的屬性獲取每個員工的Boss或其下屬。

+0

我真的可以改變我得到僱員的班級的結構,但是讓我們說我想要計算在Mike下有多少個兒童,這是可能的? – user1374734

+0

我對你的問題感到困惑。爲了讓員工成爲麥克風的子女,他們必須將他們的parentEmployeeID設置爲Mike的employeeID,而不是null。 –