Recursive call return a List, return type causing me issues

前端 未结 5 723
遥遥无期
遥遥无期 2021-02-04 06:12

I have a recursive method that is return me categories, and checking for its sub categories.

So it looks like:

public List GetAllChildCat         


        
5条回答
  •  爱一瞬间的悲伤
    2021-02-04 06:40

    I think this linq version will allow you to avoid the overhead of creating a list:

    public IEnumerable GetAllChildCats(int categoryid)
    {
        Category c = Get(categoryid);
        return new[] { c }.Concat(c.ChildCategories.SelectMany(cat => GetAllChildCats(cat)));
    }
    

    You can always call ToList() on the returned IEnumerable if you need it.

提交回复
热议问题