I have an SQL-Table which is build like a tree list (parent child). Means you have rootId's and childId's. Each entity has a ParentId. If the ParentId is null then that element is a root item.
Here an example of the hierarchical architecture
- Fruit (1)
- TreeFruits (2)
- Apple (4)
- Bulb (5)
- SeaFruits (3)
- DeepSeaFruits (6)
- Sushi (9)
- Kraken (13)
- Shrimp (7)
- Fish (8)
- Vegetable (10)
- Carrot (11)
- Potato (12)
You can see that each root element (number 1 and 10) has at least 1 child. But the deep size of elements is unknown.
Here an example what the table looks like
------------
| Id* |<---
| | |
| ParentId |----
| Name |
------------
My task is to query (with EntityFramework Core) the RootId for a given fruit. Of course I could load all Entities from db and do it in memory but that is not clever. Have you any idea how I can write my Linq that this task happens on database?