Recursive reading of List<Object>

前端 未结 5 1735
滥情空心
滥情空心 2020-12-17 07:18

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

         


        
      
      
      
5条回答
  •  没有蜡笔的小新
    2020-12-17 08:02

    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 items)
        {
            Dictionary> dictOfChildren = new Dictionary>();
            // loop through all the items grouping them according to their ParentID
            foreach (CategoryItem anItem in items)
            {
                List children;
                if (!dictOfChildren.TryGetValue(anItem.ParentID, out children))
                {
                    children = new List();
                    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 list, Dictionary> dictOfChildren, int levelOfIndentation)
        {
            foreach (CategoryItem anItem in list)
            {
                // first print the current item
                anItem.Print(levelOfIndentation);
                // then recursively print all its children
                List 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.

    EDIT:

    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 items)
    {
        // call a recursive method passing 0 as level of indentation
        PrintItems(items, 0);
    }
    
    public static void PrintItems(List 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);
        }
    }
    

提交回复
热议问题