Home » Microsoft TechnologiesRSS

Loading Hierarchical Data

I've built an Entity Data Model that includes a self-referencing table to store a hierarchical data type. The problem that I'm having is that when I load it into a TreeView and apply a HierarchicalDataTemplate, all of the records end up in the root, in addition to also being nested in their correct place.

I tried to limit the initial nodes by filtering to allow only items that don't have a parent (items that should go in the root), but then it doesn't load their children.

I can't solve this by using multiple HierarchicalDataTemplate's because I don't know at design time how deep the hierarchy goes.

Is there some way to get the entity to load the data in the right order to fill out the hierarchy?

-Thanks

 

6 Answers Found

 

Answer 1

Hello,

 

Welcome to ADO.NET entity  Framework and LINQ to Entities forum!

 

I am not an expert of WPF or Silverlight, so I don’t quite familiar with how the data  is working with HierarchicalDataTemplate.   But at the EF side, the related entities can be generally loaded through two methods: ObjectQuery<>.Include and EntityReference.Load/EntityCollection.Load.

 

E.g.  we have an self-referencing  entity named Person.   Each person has parents and children  as navigation properties.   Such a query will load  certain person, his children and his grandchildren:

================================================================================

var query = from p in context.People.Include("Children.Children") select p;

================================================================================

 

For each Person entity, we can also determine whether to load the related parent  and children:

================================================================================

if (person.ChildrenCollection.IsLoaded)

{

    person.ChildrenCollection.Load();

}

================================================================================

 

If you have any questions, please feel free to let me know.   Also please provide us with more detailed information about this question.  

 

Have a nice day!

 

 

Best Regards,
Lingzhi Sun

MSDN Subscriber Supportin Forum

If you have any feedback on our support, please contactmsdnmg@microsoft.com


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
 

Answer 2

The problem  is that it appears that Include, and load  only return the next level down in the hierachy. Does this mean that I will have to recursively walk through the entire tree to make sure that everything is loaded?
 

Answer 3

Hello,

 

If you want to load  the entire tree, I would recommend you recursively call the Load() method to retrieve all the hierarchy, as this thread suggested, http://stackoverflow.com/questions/1308158/how-does-entity-framework-work-with-recursive-hierarchies-include-seems-not-to. 

 

For the Include method, as I have suggested that we can load certain levels hierarchy  with such a query:

=====================================================================

var query = from p in context.People.Include(“Children.Children.Children”).  

=====================================================================

However, it is not possible to load the entire tree with the Include() method.   LINQ to Entities queries are translated into SQL statements, but we have no such SQL statements to include an unlimited depth in the hierarchy.  

 

Hope it helps!

 

Have a great day!

 

 

Best Regards,
Lingzhi Sun

MSDN Subscriber Supportin Forum

If you have any feedback on our support, please contactmsdnmg@microsoft.com

 

Answer 4

Thanks. I was starting to lean that direction. I ended up doing a quick and simple recursive function to walk the hierarchy  and make sure everything is loaded.

I just wanted to make sure there wasn't a better way.

Thanks!

 

Answer 5

I have a self-referencing  table with columns "Id" and "ParentId".

I was able to use EF 1.0 and DevExpress TreeList control successfully.

 

Answer 6

A common approach would be Common Table Expressions naively in SQL.
 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter