2012-12-17 1 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가 null의 어떤 직원이 없기 때문에 이것은 당신이 예를 들어, x.parentEmployeeID을 원한다고 생각 ...

어떤 결과를 반환하지 않습니다

이 당신이 원하는 것을해야 ...

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

당신은 항상 빈 목록을 추가하고 그들과 다른 아무것도하지 않는 : : 또한

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

, 이것은 더 이후하지 않습니다

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 목록이 있습니다. 당신이 진정으로 재귀 솔루션을 허용하지 않습니다 코드를 구조화하는

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

방법 : 지금 전화 ArrangeListWithRecursion()를 채 웁니다. 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; 
     } 
0

예를 들어 무엇을 달성하려고하는지 잘 모르겠습니다.

종업원 클래스 :

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 만 채울 수 있습니다. 직원의 개별 직원 개체에 대한 속성을 사용하여 각 직원의 보스 또는 부하를 얻을 수 있습니다.

+0

나는 직원을 얻는 클래스의 구조를 정말로 바꿀 수 있지만, null이있는 마이크 밑에 얼마나 많은 아이들이있을 수 있는지 계산할 수 있다고 말할 수 있습니까? – user1374734

+0

귀하의 질문에 대해 혼란스러워합니다. 직원이 마이크의 자식이 되려면 parentEmployeeID를 Mike의 employeeID로 설정하고 null이 아니어야합니다. –