Subsonic 3 and The Adjacency Model

核能气质少年 提交于 2019-12-23 04:37:05

问题


If a table has the adjacency model applied (ID,ParentID) how can the hierarchy be returned in Subsonic 3?


回答1:


All classes are partials, so create a new partial for your class (let's say it' Category) and create child collection (call it SubCategories). Then when you load your object into memory you can load the subcollection:

var allCategories=Categories.All().ToList();
allCategories.ForEach(x=>x.SubCategories=allCategories.Where(y=>y.CategoryID==x.ParentID));

That's freehanded, but that's the idea.




回答2:


I use this all the time. Just read all the rows. It's what you do with the rows later that matters. I feed them into a DevExpress tree control that automatically picks apart the ID and ParentID to show the tree. Or with a few lines of recursion you can traverse the tree and do anything you like.

var result = from p in _db.Products select p;




回答3:


This is what I have thanks to Rob but I wonder whether the recursive function is still needed? Is there a LINQ alternative?

        var allCategories = Table_1.All().ToList();
        allCategories.ForEach(x => x.Children = allCategories.Where(y => y.ParentID == x.ID));

        var topLevel = allCategories.Where(f => f.ParentID == 0);

        s.AppendLine("<ul>");
        DoStuff(topLevel);
        s.AppendLine("</ul>");

        private void DoStuff(IEnumerable<Table_1> toplevel)
        {
           foreach (var lve in toplevel)
           {
            s.AppendLine("<li>"+lve.Title);
            if (lve.Children.Count() > 0)
            {
                s.AppendLine("<ul>");
                DoStuff(lve.Children);
                s.AppendLine("</ul>");
            }
            s.AppendLine("</li>");
            }
        }


来源:https://stackoverflow.com/questions/965064/subsonic-3-and-the-adjacency-model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!