GroupBy自动映射器聚合 [英] GroupBy automapper aggregation

查看:73
本文介绍了GroupBy自动映射器聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的实体映射到新结构中.

I'm trying to map my entity into a new structure.

我的实体看起来像:

public class Settings
    {
        public int Id { get; protected set; }
        public int UserId { get; set; }
        string string Property{ get; set; }
        public string Element { get; set; }
        public string Value { get; set; }

    }

所以从数据库中会得到类似(其中值是一些基于json的值)

So from database would come something like (where value is some json based value)

UserId      Property   Element  Value
----------- ---------- -------- ------
15          std1       grid     [...]
15          std1       panel    [...]
15          std2       panel    [...]
15          std2       grid     [...]
15          std4       panel    [...]
15          std5       panel    [...]
15          std12      grid     [...]

我的目标是输出类似以下内容的结构:

My goal is to output something structured like:

{
    "std1": {
        "Elements": {
            "grid": "[...]",
            "panel": "[...]"
        }
    },
    "std2": {
        "Elements": {
            "grid":  "[...]",
            "panel": "[...]"
        }
    },
    "std4": {
        "Elements": {
            "panel": "[...]"
        }
    },
    ...
}

我创建了以下DTO以实现此目的:

I created the folling DTO's to achieve this:

public class SettingsToReturnDto
    {
        public string Domain { get; set; }
        public List<ElementsToReturnDto> Elements { get; set; }
    }

    public class ElementsToReturnDto
    {
        public string Element { get; set; }
        public string Value { get; set; }
    }
}

我尝试使用自动映射器映射来实现此目的,但是我的所有尝试都未能转换为新结构

I tried to use a automapper mapping to achieve this, but all my attemps failed into convert into the new structure

您能指出我正确的方向吗? 谢谢

can you point me into the correct direction? thanks

推荐答案

这是工作示例,您可以参考

Here is the working demo , you could refer to

设置配置文件

public class SettingsProfile:Profile
{
    public SettingsProfile()
    {
        CreateMap<IGrouping<string, Settings>, SettingsToReturnDto>()
            .ForMember(dest => dest.Domain, opt => opt.MapFrom(src => src.Key))
            .ForMember(dest => dest.Elements, opt => opt.MapFrom(src => src.ToList()));
        CreateMap<Settings, ElementsToReturnDto>();
    }
}

控制器

public class ValuesController : ControllerBase
{
    private readonly SeeMiddleContext _context;
    private readonly IMapper _mapper;

    public ValuesController(SeeMiddleContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    public IActionResult GetSettings()
    {
        List <IGrouping<string, Settings>> settingsFromDB = _context.Settings.GroupBy(s=>s.Property).ToList();

        var settingsToReturn = _mapper.Map<List<IGrouping<string, Settings>>,List<SettingsToReturnDto>>(settingsFromDB);

        return new JsonResult(settingsToReturn); 
    }
}

这篇关于GroupBy自动映射器聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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