0

Let's say we've got this table:

WORKER  
NAME     ID    ManagerID  
------------------------
John     1     3  
Sally    2     3  
Paul     3     4  
Jane     4     5  
Jennifer 5     8  

So John and Sally work for Paul, and Paul works for Jane.

For the linq query, I want to feed it Jane's ID (4) and have it return back all of her subordinates:

John     1     3  
Sally    2     3  
Paul     3     4

I would also need for this query to recursively go as deep as is needed. For example, maybe John has people working for him, so they would be included in the results too.


I was thinking about using SelectMany, but am not sure if this would work:

db.Worker.SelectMany(w => w.ID).Where(w => w.ID == 4).ToList();

I am using Linq-to-SQL and the query would only have this one table to work with.

How would you build this query?


Edit: here is what the model class would look like

WORKER  
ID          int    
Name        string  
ManagerID   int  
user7560542
  • 527
  • 1
  • 5
  • 14
  • 1
    Possible duplicate of [How to flatten tree via LINQ?](https://stackoverflow.com/questions/11830174/how-to-flatten-tree-via-linq) – DavidG May 26 '17 at 16:23
  • If you want to do this in SQL, you're going to have to manually execute some recursive SQL. – DavidG May 26 '17 at 16:41
  • can we see the model class ? – Munzer May 26 '17 at 17:20
  • Sure, just added a basic idea of what the class might look like. If you need more detail, just let me know what would help. Thanks! – user7560542 May 26 '17 at 17:38
  • 1
    I was asking for your ef model class worker – Munzer May 26 '17 at 18:05
  • as the first comment suggest , it is duplicate, if you don't understand, feel free to ask me, though i think the answer is very clear, and that guy much better than me :) – Munzer May 26 '17 at 18:16
  • Thanks Munzer. I have studied the first comment's link, but it's still not clear to me how I would implement a linq query out of this. Is there any chance you should show me what it might look like for this example? – user7560542 May 26 '17 at 18:39

0 Answers0