I am working in an ASP.NET project with EF and I have some trouble with finding a correct query. I need to work without lazy-loading.
I have following data structure: A Module contains pages. A Page contains PageItems. A PageItem contains an Item. An Item can contains other Items.
Leaving out the recursive aspect of Items containing Items at first, I need a query to feed the structure of a single module into a treeview.
What I want is :
- The Module with the given ID
- Including all pages
- Including all PageItems
- Including all Items with ParentItem_ID == 0
- Including all ChildItems
- Including all Items with ParentItem_ID == 0
- Including all PageItems
- Including all pages
I started with this:
return base._entities.Modules
.Include(m => m.Paginas
.Select(p => p.PaginaItems
.Select(pi => pi.Item)
.Select(i => i.ChildItems)))
.Where(m => m.Module_ID == id)
.FirstOrDefault();
This query works, but it ignores the item-hierarchy and displays all items as direct child of a page. What would actually need is something like this:
return base._entities.Modules
.Include(m => m.Paginas
.Select(p => p.PaginaItems
.Select(pi => pi.Item)
.Where(i => i.I_ParentItem_ID == 0)
.Select(i => i.ChildItems)))
.Where(m => m.Module_ID == id)
.FirstOrDefault();
But it does not work.
I an relatively new to LINQ, and any help would really be appreciated.
joins.