I have the following SQL Server table structure, it's a logical tree folder structure.

![SQL Server Table Structure](https://i.stack.imgur.com/5wgm4.png)

I'm using Linq to SQL with SQL Server.

What I want to do is to loop and create on memory the folder structure, the hierarchical folder from Root, the main parent to all childs.

![Folder structure](https://i.stack.imgur.com/i5cKR.png)

I'm trying to do it with some foreach loops but I still don't get it. I think I must use some kind of recursive way.

Some query I tried:

```
var flds = from folders in repo.Folders
                                   join folders2 in repo.Folders on folders.Id equals folders2.idParent
                                   select folders;

```

My goal is to iterate that folder logical structure.