2013-08-25 32 views
0
public class Group 
{ 
    public int ID; 
    public bool Earned; 
    public bool Available; 

    public List<Group> ChildGroups; 
    public List<Item> ChildItems; 
} 

public class Item 
{ 
    public int ID; 
    public bool Earned; 
    public bool Available; 
} 

public class Evaluator 
{ 
    public List<Group> FindEarned(Group source) 
    { 
     //Filter implementation 

     foreach (Group grp in source.Groups) 
     { 
      grp.Items = grp.Items.Where(
             x => x.Earned == true).ToList(); 

      if (grp.Groups != null && grp.Groups.Count > 0) 
      { 
       grp.Groups = FilterEarned(grp); 
      } 
      else 
      { 

      } 
     } 

     return source.Groups; 
    } 

} 

我找到的賺取方法應返回其中任何子組或項目處於獲得狀態的組的列表。 例子:以自下而上的方式解析嵌套對象結構

Group1 - Pending 
    -Group11 -pending 
    -Group12 -pending 
    -Group13 -Pending 
    -Item11 -Pending 
    -Item12 -Pending 
Group2 
    -Group21 -pending 
    --Group211 -pending 
    ---Item2111 - earned 
    -Group22 -pending 
    -Group23 -Pending 
    -Item21 -Pending 
    -Item22 -Pending 

方法應該返回

Group2 
    -Group21 -pending 
    --Group211 -pending 
    ---Item2111 - earned 
+0

@MarcinJuraszek added – Mahesh

+1

有什麼問題嗎? –

回答

0

我不知道我得到了你的權利,但如果你需要過濾掉所有沒有賺到項目和團體,你可以使用這個擴展方法。它並不是非常流行,因爲它必須計算Earned = true的孩子,並且它還會創建新的組,否則您的初始數據將被損壞。

public static IEnumerable<Group> EarnedGroups(this IEnumerable<Group> data) 
    { 
     foreach (var group in data) 
     { 
      var items = group.ChildItems.Where(x => x.Earned).ToList(); 
      var groups = group.ChildGroups.EarnedGroups().ToList(); 
      if (items.Count > 0 || groups.Count > 0 || group.Earned) 
       yield return new 
         Group 
         { 
          ID = group.ID, 
          Available = group.Available, 
          Earned = group.Earned, 
          ChildItems = items, 
          ChildGroups = groups 
         }; 
     } 
    }