Retrieving an entire entity tree

Posts   
 
    
peschkaj
User
Posts: 30
Joined: 21-Sep-2006
# Posted on: 16-Oct-2007 21:13:59   

I was curious if it is possible to grab an entire object graph from a self-referencing table.

E.g., I have an organization table that contains states with multiple school districts, split into multiple regions, split into multiple schools, but all are retrieved as an OrganizationEntity.

Each row has a ParentOrganizationID on it.

Thanks in advance for any help,

Jeremiah

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 16-Oct-2007 22:45:28   

there are a couple posts on this this topic. if you know the specific depths then you could use prefetch paths to build the tree. if the tree is dynamic then pull all data into memory and build the tree in business logic rather than db calls.

pat
User
Posts: 215
Joined: 02-Mar-2006
# Posted on: 07-Nov-2007 19:46:44   

peschkaj wrote:

I was curious if it is possible to grab an entire object graph from a self-referencing table.

With SQL 2005 you can use a recursive CTE (Common Table Expression) see here: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=11573&StartAtMessage=0&#64617

All the best, Patrick

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 08-Nov-2007 11:51:29   

if the tree is dynamic then pull all data into memory and build the tree in business logic rather than db calls.

Some details about this approach: - Fetch all the entities in one go. - Store them in a Dictionary or a hashtable. Store the list of child entities for each ParentId.

Example:


            Dictionary<int, List<MyEntity>> myDictionary = new Dictionary<int, List<MyEntity>>();

            foreach (MyEntity myEntity in MyEntities)
            {
                List<MyEntity> children;
                int parentId = myEntity .ParentId;

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

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

This way you can easily build a TreeView for instance using a recursive function that queries the Dictionary for the children of each node, starting from 0/null for the root node(s).

        public void SomeMethod()
        {
            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);
                }
            }   
        }