Hierarchal Data

Posts   
 
    
KenpMD
User
Posts: 2
Joined: 27-Nov-2007
# Posted on: 27-Nov-2007 21:43:19   

I have read how I can use prefetched paths and projectsion to load a graph of related entities. I have probably been over thinking this and have confused myself. I have a simple table called accounts - with a pk of accountID and a column called parentaccountid that point to the accountid which that record is related. Pretty basic stuff. In the designer I added a relationship from accountid -> parent accountid. Now I want to load an account (the root account) I want that accountentity to preload all its childer and children children so on and so on..

I'm new to LLBL so I was wondering what is the best way to configure this? There in theory can be an infinite number of levels (but in reality there should only be 5 levels of nesting).

Great product BTW - save SOOOOO much time now I don't have to do all the CRUD work.

ops forgot to mention using v2.5 and .net 2.0

mattsmith321 avatar
Posts: 146
Joined: 04-Oct-2004
# Posted on: 27-Nov-2007 22:39:48   

Check out this thread to see if it helps any: http://llblgen.com/tinyforum/Messages.aspx?ThreadID=5880

For more info, Google on "Celko" and "nested sets". For my project in the link above, I ended up implementing it with a stored proc since I couldn't quite get code to do what I wanted.

Anonymous
User
Posts: 0
Joined: 11-Nov-2006
# Posted on: 28-Nov-2007 01:50:04   

Hi Matt,

Are you using MS Sql Server 2000. If so would you you be willing to share your stored proc code. I was unable to find any online that was specific to MySql or similar.

Thanks

Pete

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 28-Nov-2007 11:27:24   

Loading a Hierarchical data from a self joined table, can be done by several ways. If you know how deep it will go, then You can easily use PrefetchPaths and SubPaths n levels deep.

Or if it has no limits, then I suggest loading the entire table into a Hashtable or a dictionary where you save each ParentID as the Key with a list of its children entities. This can be easily ustelized in building a Tree control for instance.

Example:

        private Dictionary<int, List<MyEntity>> GetDictionary()
        {
            Dictionary<int, List<MyEntity>> myDictionary = new Dictionary<int, List<MyEntity>>();
            
            EntityCollection<MyEntity> myEntities = new EntityCollection<MyEntity>();

            DataAccessAdapter adapter = new DataAccessAdapter();
            adapter.FetchEntityCollection(myEntities, null);

            foreach (MyEntity myEntity in myEntities)
            {
                List<MyEntity> children;

                if (!myDictionary.TryGetValue(myEntity.ParentId, out children))
                {
                    children = new List<MyEntity>();
                    myDictionary.Add(myEntity.ParentId, children);
                }

                if (!children.Contains(myEntity))
                {
                    children.Add(myEntity);
                }               
            }

            return myDictionary;
        }

        private void LoadTree()
        {
            Dictionary<int, List<MyEntity>> myDictionary = GetDictionary();

            TreeView1.Nodes.Clear();

            PopulateChildNodes(myDictionary, TreeView1.Nodes, 0);
        }

        private void PopulateChildNodes(Dictionary<int, List<MyEntity>> myDictionary, TreeNodeCollection childNodes, int parentId)
        {
            List<MyEntity> children = new List<MyEntity>();
            if (myDictionary.TryGetValue(parentId, out children))
            {
                foreach (MyEntity child in children)
                {
                    TreeNode childNode = new TreeNode(child.Name, child.Id.ToString());

                    PopulateChildNodes(myDictionary, childNode.ChildNodes, child.Id);

                    childNodes.Add(childNode);
                }
            }   
        }
mattsmith321 avatar
Posts: 146
Joined: 04-Oct-2004
# Posted on: 28-Nov-2007 20:40:51   

lad4bear wrote:

Are you using MS Sql Server 2000. If so would you you be willing to share your stored proc code. I was unable to find any online that was specific to MySql or similar.

Yes, we were using MS SQL 2000.

I thought about copying one of our sprocs (which I don't mind doing) here but without the context of schema and returned data (which I would object to), it would probably make it appear more complicated. As such, your best bet is to take a look at the following articles to give you more background on the nested set model: - More Trees & Hierarchies in SQL (http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=15499) - Trees in SQL (http://www.dbmsmag.com/9603d06.html) - Nested Set Model of Trees, Part 2 (http://www.dbmsmag.com/9604d06.html) - Trees in SQL, Part 3 (http://www.dbmsmag.com/9605d06.html) - Trees, Part 4 (http://www.dbmsmag.com/9606d06.html)

While the nested sets model is a little difficult to get your head wrapped around, it is a great way for handling hierarchies with an indeterminant number of levels. Of course, this mainly applies if the hierarchy is relatively static (i.e. this model supports faster reads, but slightly slower adds/updates due to the complexity of having to recalculate).