ASP.Net C# - 需要有关递归,父/子关系的帮助

时间:2011-07-22 07:59:06

标签: c# asp.net recursion

尝试在CMS系统中找到问题的优雅解决方案。如果有人移动页面,我还需要更改所有子页面。简化的数据库是这样的:

id     url              parent        level
1      page.aspx         0             0
2      page2.aspx        0             0
3      page.aspx         1             1
4      page.aspx         1             1
5      page.aspx         3             2
6      page.aspx         3             2

如果我决定将id 3移动为2而不是1,我需要更改子页面的所有url。

我可以使用简单的(ish)递归技术吗?

由于

1 个答案:

答案 0 :(得分:0)

这样的东西?

void IncrementUrlLevel(UrlCollection collection, UrlEntry entry){
    if (entry == null)
        return;

    foreach (UrlEntry childEntry in collection){
        if (childEntry.Parent == entry.Id){
            IncrementUrlLeven(collection, childEntry);
        }
    }

    entry.Level++;
}