如何创建递归类别,如下所示:
computer
computer > Lenovo
computer > Lenovo > p250
Electronic
Electronic > Lise
答案 0 :(得分:0)
我将模型设置为: 我有具有CategoryParentId的CategoryTable。如果一行具有CategoryParentId = 0,则它是第一类。 这是我的功能
private void GetSubTree(IList<Page> allCats, Page parent, List<CategoryItemModel> items)
{
var subCats = allCats.Where(c => c.CategoryParentID == parent.ID);
foreach (var cat in subCats)
{
//add this category
items.Add(new CategoryItemModel { ID = cat.Id, Title = parent.Title + " >> " + cat.Title });
//recursive call in case your have a hierarchy more than 1 level deep
GetSubTree(allCats, cat, items);
}
}