Skip to main content
 首页 » 编程设计

c#之递归读取 ListObject>

2024年02月20日13zengkefu

我的结构是 List<Object> ,具体来说是一个“CategoryItem”对象。这是我对“CategoryItem”对象的声明。

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

这是我的示例数据结构:

[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”对象:

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

这样我就可以像这样在html的无序列表中显示它

  • 欢迎NC
    • 数控
    • 制作
    • 行政
      • NPower
    • 帮助
      • 7
        • 控制
  • 首先

有办法吗?

请您参考如下方法:

如果您的 CategoryItem 不包含其子项列表(如问题的第一个版本),我首先会构建一个字典,foreach CategoryID 为您提供所有子类别项目,然后递归打印所有项目使用此字典并从父级为“0”的项目开始。假设 Print 是打印与您的项目相关的数据的指令,并且它将缩进级别作为唯一参数,代码将如下所示:

    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); 
        } 
    } 

它不是真正面向对象的,但这应该给您一个关于遵循方向的提示。

编辑:

我看到您编辑了问题,现在您已经添加了 SubCategory 属性。这让事情变得更简单,你可以简单地做:

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); 
    } 
}