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 
4

1 に答える 1

0

正しく理解できたかどうかわかりませんが、獲得していないアイテムやグループをすべて除外する必要がある場合は、この拡張メソッドを使用できます。Earned = true の子をカウントする必要があり、新しいグループも作成する必要があるため、あまり linqy ではありません。そうしないと、初期データが破損します。

    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
                       };
        }
    }
于 2013-08-25T11:14:32.380 に答える