C# 列表的递归读取<;对象>;
我有这个结构的C# 列表的递归读取<;对象>;,c#,.net,list,tree,html-lists,C#,.net,List,Tree,Html Lists,我有这个结构的列表,具体来说,它是一个“CategoryItem”对象。这是我对“CategoryItem”对象的声明 我的问题是,我如何介入我声明的每个“CategoryItem”对象: List<CategoryItem> categoryItems = new List<CategoryItem>(); List categoryItems=new List(); 这样我就可以在一个无序的html列表中显示它,如下所示 欢迎 数控 生产 行政的 NPowe
列表
,具体来说,它是一个“CategoryItem”对象。这是我对“CategoryItem”对象的声明
我的问题是,我如何介入我声明的每个“CategoryItem”对象:
List<CategoryItem> categoryItems = new List<CategoryItem>();
List categoryItems=new List();
这样我就可以在一个无序的html列表中显示它,如下所示
- 欢迎
- 数控
- 生产
- 行政的
- NPower
- 帮助
- 七,
- 奇克
- 七,
- 首先
有没有办法做到这一点?请仔细阅读。那里有不少样品。维基百科上的方法很简单,但原理是一样的:在一个“级别”上解决问题,然后在每个子级别上递归调用相同的方法。继续阅读。那里有不少样品。Wikipedia上的方法很简单,但原理是一样的:在一个“级别”上解决问题,然后在每个子级别上递归调用相同的方法。也许可以看看TreeView控件 ASP.NET TreeView控件旨在以层次结构向用户提供数据。用户可以打开各个节点,这些节点又可以包含子节点。TreeView控件适用于显示XML数据,但可以用于可以在层次结构中表示的任何数据 我想这将是你想要的
否则,就需要遍历列表中的每个项,递归地处理每个子项,这些子项可能有更多的子项,也可能没有更多的子项。这很棘手,但只是刚开始 可以看看TreeView控件吗 ASP.NET TreeView控件旨在以层次结构向用户提供数据。用户可以打开各个节点,这些节点又可以包含子节点。TreeView控件适用于显示XML数据,但可以用于可以在层次结构中表示的任何数据 我想这将是你想要的
否则,就需要遍历列表中的每个项,递归地处理每个子项,这些子项可能有更多的子项,也可能没有更多的子项。这很棘手,但只是刚开始 您需要创建一个“呈现”类别项的递归方法。此方法需要了解级别或当前深度,以便渲染到正确的缩进:
private void RenderCategory(HtmlTextWriter writer, CategoryItem item, int level)
{
writer.Write("<li style='padding-left: {0}px'>{1}</li>",
level * 5,
HttpUtility.HtmlEncode(item.Name));
int nextLevel = ++level;
foreach (CategoryItem child in item.SubCategories)
{
RenderCategory(writer, child, nextLevel);
}
}
private void RenderCategory(HtmlTextWriter编写器,CategoryItem项,int级别)
{
Write.Write(“{1} ”,
级别*5,
HttpUtility.HtmlEncode(item.Name));
int nextLevel=++级;
foreach(项目子类别中的CategoryItem子类别)
{
渲染分类(作家、儿童、下一级);
}
}
您需要创建一个“呈现”类别项的递归方法。此方法需要了解级别或当前深度,以便渲染到正确的缩进:
private void RenderCategory(HtmlTextWriter writer, CategoryItem item, int level)
{
writer.Write("<li style='padding-left: {0}px'>{1}</li>",
level * 5,
HttpUtility.HtmlEncode(item.Name));
int nextLevel = ++level;
foreach (CategoryItem child in item.SubCategories)
{
RenderCategory(writer, child, nextLevel);
}
}
private void RenderCategory(HtmlTextWriter编写器,CategoryItem项,int级别)
{
Write.Write(“{1} ”,
级别*5,
HttpUtility.HtmlEncode(item.Name));
int nextLevel=++级;
foreach(项目子类别中的CategoryItem子类别)
{
渲染分类(作家、儿童、下一级);
}
}
一个简单但不是最优的解决方案是,只需迭代列表,并计算每个项目在0之前调用其父id的次数。这样,您就知道您的
必须具有多少级别的标识。一个简单的、非最优但简单的解决方案就是迭代列表,并计算每个项目在0之前调用其父id的次数。这样你就知道你的
必须有多少识别级别了。如果你的分类项目不包含它的子项列表(如问题的第一个版本),我首先会为每个分类项目建立一个字典,为你提供所有的子分类项目,然后使用此字典递归打印所有项目,并从父级为“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);
}
}
公共静态无效打印项(列表项)
{
Dictionary dictOfChildren=新字典();
//循环遍历所有项,并根据它们的ParentID对它们进行分组
foreach(项目中的类别项目)
{
列出儿童名单;
if(!dictOfChildren.TryGetValue(anItem.ParentID,out children))
{
children=新列表();
儿童口述[anItem.ParentID]=儿童;
}
添加(anItem);
}
//递归打印从ParentID=0的项目开始的所有项目
//字典被传递给方法,以便能够找到每个项的子项
打印项目(dictOfChildren[“0”],dictOfChildren,0);
}
私有静态void打印项(列表列表、儿童词典口述、int-levelof-indentation)
{
foreach(列表中的类别项目)
{
//首先打印当前项目
项目打印(凹痕水平);
//然后递归打印其所有子项
列出儿童名单;
if(dictOfChildren.TryGetValue(anItem.CategoryID,out children)&&
儿童。计数>0)
打印项目(儿童、儿童口述、印象等级+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);
}
}
公共静态无效打印项(列表项)
{
//调用将0作为le传递的递归方法
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);
}
}