开发者

ASP.Net C# - need help with recursion, parent/child relationship

开发者 https://www.devze.com 2023-03-22 08:48 出处:网络
Trying to find an elegant solution for a problem in my CMS system.If someone moves a page, I need all the child pages to also be changed.Simplified database is like this:

Trying to find an elegant solution for a problem in my CMS system. If someone moves a page, I need all the child pages to also be changed. Simplified database is like this:

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.asp开发者_如何学编程x         3             2

If I decide to move id 3 to have 2 as a parent instead of 1, I need to change all of the url's for the child pages.

Is there a simple(ish) recursive technique I can use for this?

Thanks


Something like this?

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++;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消