List<Object>的递归读取 [英] Recursive reading of List<Object>
问题描述
我有这种 List
结构,具体来说它是一个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
- 欢迎NC
- 数控
- 生产
- 行政
- NPower
- 7
- CHLK
有没有办法做到这一点?
Is there a way to do this?
推荐答案
如果您的 CategoryItem 不包含其子项的列表(如问题的第一个版本),我将首先构建一个用于 foreach 的字典CategoryID 为您提供所有子类别项目,然后使用此字典递归打印所有项目,并从父项为0"的项目开始.假设 Print 是打印与您的项目相关联的数据的指令,并且它将缩进级别作为唯一参数,代码将如下所示:
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.
我看到您编辑了问题,现在您已经添加了 SubCategory 属性.这让事情变得更简单,你可以简单地做:
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); } }
这篇关于List<Object>的递归读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!