【发布时间】:2011-11-03 11:40:33
【问题描述】:
在一个 asp.net 应用程序中,我有一个类别对象列表,在此列表中,每个类别都可以是另一个类别的父类别。
例子:
catid 1 catname cat1 parentid null
catid 2 catname cat2 parentid null
catid 3 catname cat3 parentid 2
catid 4 catname cat4 parentid 2
catid 5 catname cat5 parentid 4
catid 6 catname cat6 parentid 5
catit 7 catname cat7 parentid 5
我想写一个方法,循环遍历类别列表,拉出父类别并从列表中获取子类别。 这样做很容易,但我遇到的困难部分是我如何知道在递归方法中何时到达最后一个类别对象。
这就是我要寻找的逻辑
protected void load_categories(ref List<category> list, category item)
{
//loop through list and match item ID with list item parent ID
//loop through child items of category item using load_categories()
//HOW DO I STOP ONCE EVERYTHING IS DONE?
}
【问题讨论】: