-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for querying a hierarchy of self-referencing entities? #3241
Comments
Your best bet would be: This will pull affiliates along with their children and grandchildren. For one-to-one navigations you can use: This will pull patent and grandparent. The second case will be done with one query to the database. In case of one-to-many there will be multiple queries, one per include level. |
In EF6 you would do a similar thing: There is no way in either EF6 or EF7 to automatically retrieve the entire hierarchy. |
@maumar thanks. that's awesome. this way I can even do this recursively. i was only wondering if |
@weitzhandler yes, you can compose on top of Include like this: context.Affiliates.Include(a => a.Referred).Where(a => a.Id >5), but you can't so stuff like: |
I hope that will be implemented too. Anyway it can be achieved if the selection is on the However, I'm still not sure how to achieve recursive inclusion of self-referential entities: I want to achieve
which returns processes the base query and processes the base expression to add a I've done it the ugly hardcoded way, but should work (didn't test yet):
See this forum. There is a demand for this. I really hope it gets in to the EF. |
@weitzhandler currently, the Include(string) is supposed to be used for dynamic include generation, just like you did in the example above. Another way is to use ThenInclude() repeatedly for each consecutive level on navigation property (this can be done on both 1-1 and 1-many), e.g.: Affiliates.Include(a => a.Referred).ThenInclude(a => a.Referred).ThenInclude(...) Alternatively one could hand-craft a lambda expression and pass it to the Include, although that is arguably more messy than using any of the above methods. Finally, if you are able to determine all the entities that are part of the hierarchy you need (e.g if you are using hierarchyid), you can just query for those, and EF will hook up references between them automatically. |
@maumar Yeah, I initially tried to achieve this with expressions, but once I found the string version I decided to got for it... Appreciate your responses! |
@weitzhandler yes, ThenInclude if EF7 API, mainly to allow chaining includes on collections in a strongly typed fashion. |
@maumar oh, so you sayin that in EF6 it's only possible out-the-box with the hardcoded version? |
@weitzhandler correct, you need to hand-craft either a string or a lambda expression |
@maumar Thanks for all the info! |
The problem begins when you want to select children instead of parents, or you want to select a predefined view of children, i.e. all children in level 3 and 4, along with their phones. |
Hi guys, just for you to know, I am using EF6 and have a hierarchy structure that need to load in some special case. At first I tried to load the full hierarchy using the "Include(string)" building the string with a loop like weitzhandler sugested. But then I decided to just load the sub-elements using the load method. It ends up being much quicker with the load method, probably because I am just asking for the one more level that I really need for each of the tree nodes in the hierarchy. But using a string, I might ask for much more that I really need. On the other hand, the string does not ensure to load the full hierarchy. Still I understand that it might be dangerous to load the full hierarchy and should only be used in controlled cases. This the the code I wrote (owner is the father of a hierarchy of groups and sub-groups):
} private void LoadGroups(Group group)
} |
@alvaromongon this actually helped me, you just need to be careful not to create a loop in db.
|
A trick I've used before is to maintain another table with a [parent, depth, sort, child] record for every relationship between ancestors and descendants. Then you can query this table to either return all parents sorted by depth, or all children sorted in a appropriate order to display in a tree structure. |
I know this thread is old but it pointed me in the right direction.
|
How to calculate the depth of each level of the tree ? |
In this case you're walking backwards in the hierachy when you are loading it, so it's not possible to know the depth during this phase. |
Hi,
Often you need to retrieve a whole hierarchy of self-referencing entities.
Consider this example:
Now I want to retrieve an
Affiliate
along with its children, grandchildren, and great grandchildren. Or I want to retrieve anAffiliate
with itsReferrer
, he's referrer's referrer, and his referrer's referrer's referrern
levels up.How can this be achieved in a single round-trip without involving stored-procedures?
The text was updated successfully, but these errors were encountered: