名单&LT递归阅读;对象> [英] Recursive reading of List<Object>

查看:171
本文介绍了名单&LT递归阅读;对象>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有的这种结构名单,其中,对象> ,具体它是一个CategoryItem对象。这是我的声明CategoryItem的对象。

I have this structure of List<Object>, to be specific it is a "CategoryItem" Object. Here's my declaration of "CategoryItem" Object.

public class CategoryItem
{
    public string Name { get; set; }
    public int CategoryID {get; set;}
    public int ParentID {get; set; }
    public List<CategoryItem> SubCategory {get; set;}
}

下面是我的样本数据结构:

Here's my sample data structure:

[0] CategoryID: 249
Name: "WelcomeNC"
ParentID: 0
SubCategory: length=4
    [0] CategoryID: 250
        Name: "CNC"
        ParentID: 249
        SubCategory: length=0
    [1] CategoryID: 251
        Name: "Production"
        ParentID: 249
        SubCategory: length=0
    [2] CategoryID: 252
        Name: "Administrative"
        ParentID: 249
        SubCategory: length=1
            [0] CategoryID: 261
                Name: "NPower"
                ParentID: 252
                SubCategory: length=0
    [3] CategoryID: 253
        Name: "Help"
        ParentID: 249
        SubCategory: length=1
            [0] CategoryID: 254
                Name: "7"
                ParentID: 253
                SubCategory: length=1
                    [0] CategoryID: 255
                        Name: "CHLK"
                        ParentID: 254
                        SubCategory: length=0
[1] CategoryID: 111
Name: "First"
ParentID: 0
SubCategory: length=0

我的问题是,我怎么踩在每对每一个CategoryItem对象我宣布:

My problem is, how do I step in each and every 'CategoryItem' object of my declared:

List<CategoryItem> categoryItems = new List<CategoryItem>();

所以,我可以在HTML像这样在一个无序列表中显示它

So that I can display it in an unordered list in html like this

  • WelcomeNC
    • CNC
    • 生产
    • 管理
      • 甲级
      • WelcomeNC
        • CNC
        • Production
        • Administrative
          • NPower
          • 7
            • CHLK
            • 7
              • CHLK

              有没有办法做到这一点?

              Is there a way to do this?

              推荐答案

              如果您CategoryItem不包含其子列表(如在问题的第一个版本),我将首先建立一个字典是的foreach类别ID给你所有的子类别的项目,然后递归打印您的所有使用该字典,并开始与父母0的项目的项目。假设打印是打印与您的项目相关的数据的指令,而且,它需要作为唯一的参数缩进的水平,code将是这样的:

              If your CategoryItem doesn't contain a list of its children (like in the first version of the question), I would first of all build a Dictionary that foreach CategoryID gives you all the subcategory items, then recursively print all your items using this dictionary and starting with the items with parent "0". Assuming that Print is the instruction that print the data associated with your item, and that it takes as the only parameter the level of indentation, the code will look like this:

                  public static void PrintItems(List<CategoryItem> items)
                  {
                      Dictionary<string, List<CategoryItem>> dictOfChildren = new Dictionary<string, List<CategoryItem>>();
                      // loop through all the items grouping them according to their ParentID
                      foreach (CategoryItem anItem in items)
                      {
                          List<CategoryItem> children;
                          if (!dictOfChildren.TryGetValue(anItem.ParentID, out children))
                          {
                              children = new List<CategoryItem>();
                              dictOfChildren[anItem.ParentID] = children;
                          }
                          children.Add(anItem);
                      }
                      // recursively print all the items starting from the ones with ParentID = 0
                      // the dictionary is passed to the method in order to be able to find the children of each item
                      PrintItems(dictOfChildren["0"], dictOfChildren, 0);
                  }
              
                  private static void PrintItems(List<CategoryItem> list, Dictionary<string, List<CategoryItem>> dictOfChildren, int levelOfIndentation)
                  {
                      foreach (CategoryItem anItem in list)
                      {
                          // first print the current item
                          anItem.Print(levelOfIndentation);
                          // then recursively print all its children
                          List<CategoryItem> children;
                          if (dictOfChildren.TryGetValue(anItem.CategoryID, out children) &&
                              children.Count > 0)
                              PrintItems(children, dictOfChildren, levelOfIndentation + 1);
                      }
                  }
              

              这不是真正的面向对象的,但是这应该给你遵循的方向提示。

              It's not really object oriented, but this should give you a hint about the direction to follow.

              编辑:

              我看到你编辑的问题,现在你已经添加子类别属性。这使事情变得简单,你可以简单地做:

              I saw that you edited the question and that now you have added the SubCategory property. This makes things much simpler and you can simply do:

              public static void PrintItems(List<CategoryItem> items)
              {
                  // call a recursive method passing 0 as level of indentation
                  PrintItems(items, 0);
              }
              
              public static void PrintItems(List<CategoryItem> items, int levelOfIndentation)
              {
                  foreach (CategoryItem anItem in items)
                  {
                      // print the currentItem
                      anItem.Print(levelOfIndentation);
                      // increment the level of indentation and callk the same method for the children
                      PrintItems(anItem.SubCategory, levelOfIndentation + 1);
                  }
              }
              

              这篇关于名单&LT递归阅读;对象&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆