asp.net-core - GroupBy 自动映射器聚合

标签 asp.net-core automapper

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

我的实体看起来像:

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 的值)

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     [...]

我的目标是输出如下结构的内容:

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

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

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; }
    }
}

我尝试使用自动映射器映射来实现这一点,但我所有的尝试都无法转换为新结构

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

最佳答案

这是工作演示,你可以引用

设置配置文件

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>();
    }
}

Controller

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); 
    }
}

关于asp.net-core - GroupBy 自动映射器聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57322313/

相关文章:

c# - ASP.NET核心MVC : split hosting and business-logic/ui into separate projects

c# - 递归模型的 AutoMapper 条件映射

c# - 自动映射器创建新实例而不是映射属性

asp.net-core - 无法使用 nginx 访问托管在 ubuntu 上的网站

asp.net-core - .Net-Core 从 httpContext 获取 HttpBrowserCapabilities

asp.net-core - EF 7 Beta 8 -> EF7 RC1 - 不再可以创建迁移

asp.net-mvc - 当您的 View 模型没有与域模型一样多的字段时,您如何忽略/保留 MVC 中的值?

c# - AutoMapper IsSourceValueNull 条件不起作用

c# - AutoMapper.Map 忽略源对象中的所有 Null 值属性

asp.net-core - asp.net core 中 NotFoundObjectResult 的自定义页面