要使用C#查找子类别?

时间:2014-03-14 21:24:49

标签: c# .net

在应用程序中,类别可以包含子类别。每个子类别都可以包含子类别。类别也可以是一个或多个类别的子类别。如果我们给出了Category class.how,我们可以实现一个属性,该属性返回一个类别及其所有UNIQUE子类别的UNIQUE子类别的数量吗?

代码段

public class Category
{
 public List<Category> Subcategories = new List<Category>();
 public int UniqueSubcategoriesCount
 {
  get
  {
    //How to implement
    /*My Thoughts.
     1. use the CategoryID field to find the unique ones.
     2. Implement the Equals()function to   compare the CategoryID.
     3. To find the subcategories with in the categories we need to loop recursively./*
  }
 }
}

欢迎任何其他想法。

2 个答案:

答案 0 :(得分:0)

根据我对你的问题的理解,你可以尝试这样的事情:

//Category implementation
public class Category
{
    public List<Category> SubCategories { get; set; }
    public int CategoryID { get; set; }

    public static int count = 0;

    public Category()
    {
        SubCategories = new List<Category>();
    }

    public void Add(Category cat)
    {
        SubCategories.Add(cat);
    }

    public IEnumerable<Category> AllSubCategories()
    {
        var stack = new Stack<Category>();
        stack.Push(this);

        while (stack.Count > 0)
        {
            Category cat = stack.Pop();
            yield return cat;

            foreach (Category nextCat in cat.SubCategories)
                stack.Push(nextCat);
        }

    }

}

...

//Testing the code
Category top = new Category() { CategoryID = 1 };
top.Add(new Category() { CategoryID = 2 });
top.Add(new Category() { CategoryID = 5 });
top.Add(new Category() { CategoryID = 3 });
top.Add(new Category() { CategoryID = 1 });
top.Add(new Category() { CategoryID = 2 });
top.Add(new Category() { CategoryID = 4 });

Category tmp = new Category() { CategoryID = 1 };
top.Add(tmp);

tmp.Add(new Category() { CategoryID = 1 });
tmp.Add(new Category() { CategoryID = 7 });
tmp.Add(new Category() { CategoryID = 6 });

Category tmp2 = new Category() { CategoryID = 7 };
tmp.Add(tmp2);

Category tmp3 = new Category() { CategoryID = 4 };
tmp2.Add(tmp3);

Category tmp4 = new Category() { CategoryID = 3 };
tmp3.Add(tmp4);

Category tmp5 = new Category() { CategoryID = 1 };
tmp4.Add(tmp5);

Category tmp6 = new Category() { CategoryID = 9 };
tmp5.Add(tmp6);

var Result = top.AllSubCategories().GroupBy(c => c.CategoryID).Select(g => g.First()).ToList();

干杯

编辑#1:我认为我应该赞扬Jon Skeet这个想法:https://stackoverflow.com/a/2055946/172769

以防万一有人想知道它来自哪里。

编辑#2:根据Eric Lippert关于嵌套迭代器性能不佳的评论,我改变了我的代码,使用&#34;展开堆栈&#34;我得到的方法:https://stackoverflow.com/a/1043363/172769

答案 1 :(得分:0)

这样做你想要的吗?

public class Category
{
    public int CategoryID;
    public List<Category> Subcategories = new List<Category>();
    public int UniqueSubcategoriesCount
    {
        get
        {
            return this.GetUniqueSubcategories().Count();
        }
    }

    private IEnumerable<Category> GetUniqueSubcategories()
    {
        return
            this
                .GetSubcategories()
                .ToLookup(x => x.CategoryID)
                .SelectMany(xs => xs.Take(1));
    }

    private IEnumerable<Category> GetSubcategories()
    {
        return this.Subcategories
            .Concat(this.Subcategories
                .SelectMany(x => x.GetSubcategories()));
    }
}