简体   繁体   English

自引用列表项从父级到最后一个子级的计数

[英]Self Referencing list item count from parent to to last child

i have a self referencing list of categories , where each category have list of children and in the same time have a list of items . 我有一个类别的自引用清单,其中每个类别都有一个子清单,同时有一个清单。 example category1 (has 3 items, has 1 children subcategory ) subcategory (has 1 item,has parent category1, has no children ) when i fetch the list of categories i included the item count , so i am expecting to get with category1 ( itemcount = 4 ) but what i am getting is 3 示例category1(有3个项目,有1个孩子子类别)子类别(有1个项目,有父category1,没有孩子),当我获取类别列表时,我包括了项目数,因此我希望得到category1(itemcount = 4)但我得到的是3

public class Category {
    public int Id { get; set; }
    public string Name { get; set; }
    public int? ParentId { get; set; }
    public virtual Category Parent { get; set; }
    public virtual ICollection<Category> Children { get; set; }
    public ICollection<Item> Items { get; set; }
}

and below is the dto 下面是dto

public class CategoryForReturnDto
{
     public string Name { get; set; }
     public int Id { get; set; }
     public int ItemsCount { get; set; }
     public ICollection<Category> Children { get; set; }
     public int ParentId { get; set; }
}

and finaly the automapper 最后是自动映射器

        CreateMap<Category, CategoryForReturnDto> ()
            .ForMember (dest => dest.ItemsCount, opt => {
                opt.MapFrom (src => src.Items.Count);
            });

the item model is 项目模型是

public class Item {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public ICollection<ItemPhoto> Photos { get; set; }
    public Category Category { get; set; }
    public int CategoryId { get; set; }
}

DB example: 数据库示例:

在此处输入图片说明

在此处输入图片说明

You say yourself 你说你自己

example category1 (has 3 items, has 1 children subcategory ) 示例category1(有3个项目,有1个子类别)

so ItemCount correctly returns 3. That's exactly what you're telling it to return: 因此ItemCount正确返回3。这就是您要告诉它返回的内容:

opt.MapFrom (src => src.Items.Count);

I don't see any collection or property in the DTO for SubCategories; 我在DTO中看不到SubCategories的任何集合或属性; until you get them included, you can't get the number you want. 直到您将它们包括在内,您才能获得所需的号码。 (I'm not sure what you would call the number you want; ItemCount is misleading.) (我不确定您会叫什么电话号码; ItemCount有误导性。)

the solution was to add a function 解决方案是添加一个功能

    static int RecursiveItemsCount (Category cat, int count) {
        count += cat.Items.Count ();
        foreach (var child in cat.Children) {
            count += RecursiveItemsCount (child, 0);
        }
        return count;
    }

and inside the automapper 在自动映射器内部

        CreateMap<Category, CategoryForReturnDto> ()
            .ForMember (dest => dest.ItemsCount, opt => {
                int number = 0;
                opt.ResolveUsing (src => {
                    return RecursiveItemsCount (src, number);
                });
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM