1

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?

Jorge
  • 17,164
  • 18
  • 79
  • 125

3 Answers3

1

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

Community
  • 1
  • 1
eouw0o83hf
  • 9,080
  • 4
  • 55
  • 70
1

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)

FMM
  • 4,219
  • 1
  • 24
  • 44
0

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.

Amy B
  • 105,294
  • 20
  • 131
  • 182