在Entity Framework 6中投影自引用多级实体 [英] Projecting self referencing multi level Entities In Entity Framework 6

查看:70
本文介绍了在Entity Framework 6中投影自引用多级实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Entity Framework 6中投影自引用多级实体.

Projecting self referencing multi level entities in Entity Framework 6.

假设我有一个Category实体,如下所示:

Let's say that I have a Category entity as follows:

public class Category
{
    public int CategoryId { get; set; }
    public int? ParentCategoryId { get; set; }        
    public string Name { get; set; }
    public string Description { get; set; }        

    public virtual Category ParentCategory { get; set; }

    public virtual ICollection<Category> SubCategories { get; set; }
    public virtual ICollection<Product> Products { get; set; }

    public Category()
    {            
        SubCategories = new HashSet<Category>();
        Products = new HashSet<Product>();
    }
}

我想将所有层次结构的整个Category DbSet映射到以下POCO类(同时包括子类别和父类别的所有可能级别):

And I would like to map the whole Category DbSet with all the hierarchy to a following POCO class (while including all possible levels of sub and parent categories):

public class CategoryView
{
    public int Id { get; set; }
    public int? ParentCategoryId { get; set; }        
    public string Name { get; set; }
    public string Description { get; set; }        

    public CategoryView ParentCategory { get; set; }

    public List<CategoryView> SubCategories { get; set; }

    public int ProductCount { get; set; }

    public Category()
    {            
        SubCategories = new HashSet<CategoryView>();            
    }
}

请记住,一个类别可能具有无限级别的子类别,如下所示:

Please bear in mind that a single category may have unlimited levels of subcategories as follows:

Category (Level 0)
    SubCategory1 (Level 1)
    SubCategory2
        SubCategory2SubCategory1 (Level 2)
        SubCategory2SubCategory2
            SubCategory2SubCategory2SubCategory1 (Level 3)
            ... (Level N)
    SubCategory3

当尝试使用递归创建层次结构时,尝试处理每个单个类别子类别和父类别的方法得到stackoverflow exception,因为它卡在第一类别(Category)和第一子类别(SubCategory1)之间),原因是ParentCategorySubCategories之间的关系.

When tried to create hierarchy with recursive a method which tries to process every single categories sub and parent categories, got stackoverflow exception, since it get stuck between the first category (Category) and the first subcategory (SubCategory1) due to relation between ParentCategory and SubCategories.

进行这种投影的最佳且优雅的方法是什么(不消除父母)? (或者有吗?)

What is the best and elegant way of doing such projection (without eliminating parents)? (Or is there any?)

任何帮助将不胜感激.

谢谢

推荐答案

我不能说这是最好还是优雅的方法,但这是构建这种结构的相当标准和有效的非递归方法.

I can't say if it's the best or elegant way, but it's pretty standard and efficient non recursive way of building such structure.

使用简单的投影开始,在没有父/子对象链接的情况下加载所有类别 :

Start with loading all categories without parent / child object links using a simple projection:

var allCategories = db.Categories
    .Select(c => new CategoryView
    {
        Id = c.CategoryId,
        ParentCategoryId = c.ParentCategoryId,
        Name = c.Name,
        Description = c.Description,
        ProductCount = c.Products.Count()
    })
    .ToList();

然后创建一个快速查找数据结构,以通过Id查找CategoryView:

then create a fast lookup data structure for finding CategoryView by Id:

var categoryById = allCategories.ToDictionary(c => c.Id);

然后使用先前准备的数据结构将子类别链接到其父类别:

then link the subcategories to their parents using the previously prepared data structures:

foreach (var category in allCategories.Where(c => c.ParentCategoryId != null))
{
    category.ParentCategory = categoryById[category.ParentCategoryId.Value];
    category.ParentCategory.SubCategories.Add(category);
}

此时,树链接已准备就绪.根据您的需求.如果需要真实的树表示,则返回allCategories或根类别:

At this point, the tree links are ready. Depending of your needs. either return the allCategories or the root categories if you need a real tree representation:

return allCategories.Where(c => c.ParentCategoryId == null);

P.S.实际上,可以避免使用allCategories列表,因为categoryById.Values可以达到相同的目的.

P.S. Actually the allCategories list can be avoided, since categoryById.Values could serve the same purpose.

这篇关于在Entity Framework 6中投影自引用多级实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆