In this particular case I think a recursive property might be a good bet. Trying to do this from memory (been years), performance won't be great. NO lazy loading and NO explicit loading.
public class Category {
public int Id {get; set;}
// other stuff
public List<Category> MyChildren {
get { return _context.Categories.Where(x => x.ParentCategoryId == Id).ToList<Category>(); }
}
}
This should give you a hierarchical graph starting with a given node.
var justOne = _context.Categories.FirstOrDefault(x => x.Id = <myval>);
Downside is you will have to parse/use the result recursively and potentially it grows exponentially.
Clarification: The use of _context in the recursion is not allowed by EF and was used for illustration purposes. In the repository/UoW or business layer you could use the same technique to "assemble" the finished entity by having a property of a method that calls the method recursively.
Just for fun, here's the same recursion technique (but not as a property, don't have time right now).
public class Category // EF entity
{
public int Id { get; set; }
public int ParentId { get; set; }
public virtual List<Category> MyChildren { get; set; }
}
public static class MVVMCategory
{
public static Category GetCategory(int id)
{
Category result = _context.Categories.FirstOrDefault(x => x.Id == id);
result.MyChildren = GetChildren(id);
return result;
}
public static List<Category> GetChildren(int id)
{
List<Category> result = _context.Categories.Where(x => x.ParentId == id).ToList<Category>();
foreach (var item in result)
{
item.MyChildren = GetChildren(item.Id);
}
return result;
}
}
One thing to notice. I added virtual to the list for lazy loading because I am loading children "by hand" so to speak.