Entity Framework Recursive Relationship Hierarchical Data

asp.net-mvc asp.net-mvc-5 c# entity-framework entity-framework-6

Question

Does Entity Framework support some kind of recursive LINQ or do I have to write my queries in SQL? (Using ParentId - Typical category subcategory problem)

1
0
12/24/2017 11:06:52 AM

Popular Answer

I think you can solve it by using Include method inside extension method to get only piece of hierarchy or get it all. Even it can generate pretty ugly SQL.

using(var context = new HierarchyContext())
{
    var depth = context
                .Categories
                .IncludeHierarchy(3, nameof(Category.Children));

    var root = depth.Single(c => c.Id == 2);
}


public static IQueryable<T> IncludeHierarchy<T>(this IQueryable<T> source, 
uint depth, string propertyName)
where T : Category
{
    var temp = source;

    for (var i = 1; i <= depth; i++)
    {
        var sb = new StringBuilder();

        for (var j = 0; j < i; j++)
        {
            if (j > 0)
            {
                sb.Append(".");
            }

            sb.Append(propertyName);
        }

        var path = sb.ToString();

        temp = temp.Include(path);
    }

    var result = temp;

    return result;
}

public class Category
{    
   // Primary key    
   public int Id { get; set; }

   // Category name    
   public string Name { get; set; }

   // Foreign key relationship to parent category    
   public int ParentId { get; set; }

   // Navigation property to parent category    
   public virtual Category Parent { get; set; }

   // Navigation property to child categories    
   public virtual ICollection<Category> Children { get; set; }    
}
0
12/24/2017 3:27:11 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow