Stack Exchange
How do I avoid recursive object fetching with C# and Entity Framework
Right now, for accessing all child categories of a self joining object I am making query like this.
var productCategories = db.Categories.Inlcude("ChildCategories")
            .Inlcude("ChildCategories.ChildCategories")
            .Inlcude("ChildCategories.ChildCategories.ChildCategories")
            .Inlcude("ChildCategories.ChildCategories.ChildCategories.ChildCategories")
            .Inlcude("ChildCategories.ChildCategories.ChildCategories.ChildCategories.ChildCategories")
            .Inlcude("ChildCategories.ChildCategories.ChildCategories.ChildCategories.ChildCategories.ChildCategories")

What can be done to about this kind of query?

Well possibly a db.Categories.ToList() will do, because it will fetch all Categories. But i have to admit, i have no idea how to do this, if you need a where() ...

To allow the IQueryable to fetch everything (opposite of Lazy Loading), you can disable Lazy Loading on the DBContext.
 using(DBContext db = new DBContext) {
      db.ContextOptions.LaxyLoadingEnabled = false;
      // TODO: Other code here
 }

Edit: Fixed answer in response to @Slauma comment.
In the case that ChildCategory is a child class of Category (inheritance):
If you want the IQueryable to fetch everything for a ChildCategory, you can use the OfType<T>() method.
 var productCategories = db.Categories.OfType<ChildCategories>();


Why OfType? It is for filtering derived entities, but ChildCategories is a navigation collection of the same type as Category, nothing is derived here.

@Slauma Right, my mistake, when I see the word Child, I think inheritance.
