Reputation: 18257
So let's say that I have a table with a relation with itself, for example
Table Page
-------------
IDPage
description
IDPage_FK <-- foreign key
Now this table it's mapped by entity framework like this
Class Page
-----------
int IDPage
string description
int IDPage_FK
Page Page1
IColletion<Page> Page2
What I want to archive if it's possible, it's created a linq expression to navigate on all the table an make an output like this in a string variable:
Assuming this values in the table
IDPage Description IDPage_FK
1 Example null
2 Example2 1
3 Example3 2
This output on a string variable
string inheritance = (from P in Page Select....)
The output will be like this
Example > Example2 > Example3
It's possible? or Do I have to create a method to loop into each element an create the variable?
Upvotes: 1
Views: 299
Reputation: 110221
If you add a root node property into your model, you can query two levels: root nodes, all their children by root node id. Then you can recurse against the in-memory instances to produce your string.
Upvotes: 0
Reputation: 4329
Have you considered the nested set model rather than the parent pointer for mapping hierarchical relationships in a database table?
(Granted this isn't an answer, but may guide you down a better road long term)
Upvotes: 1
Reputation: 9598
Sorry, you can't do recursive Linq out to a database - you'll probably either have to write an iterative function or create a stored procedure to do it for you.
Similar question:
How does Entity Framework work with recursive hierarchies? Include() seems not to work with it
Upvotes: 1