c# - 使用 AutoMapper 将字典的值映射到列表

标签 c# automapper

我有一个数据库对象,它有一个属性字典,我需要将其映射到新对象中的列表对象。我的结构(简化)如下所示:

public class DbObject { // The source
  public Dictionary<string, DbCustomProperty> customProperties;
}

public class DbCustomProperty {
  public string Key;
  public string Value;
}



public class DTOObject { // The destination
  public List<DTOCustomProperty> DTOCustomProperties;
}

public class DTOCustomProperty {
  public string Key;
  public string Value;
}

如您所见,我想将 DbObject(源)映射到 DTOObject(目标)。问题是我的 DBObject 包含一个字典,其中的值是我想映射到列表中的属性。

例如。 dBObject.CustomProperties.Values --> dtoObject.CustomProperties

这可以通过 AutoMapper 实现吗?

最佳答案

您可以使用:

AutoMapper.CreateMap<IGrouping<string, DbCustomProperty>, DTOCustomProperty>()
            .ForMember(dest => dest.Key, opt => opt.MapFrom(src => src.Key))
            .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.Value)));

关于c# - 使用 AutoMapper 将字典的值映射到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39705044/

相关文章:

automapper - Automapper可以从属性字典映射到平面目标吗?

c# - 字符串表列表 c#

c# - 如何从 IronRuby 调用 C# IEnumerable

c# - .Wait() 和 .GetAwaiter().GetResult() 有什么区别?

automapper - 向 Unity 注册 AutoMapper 失败

.net - AutoMapper 配置文件和单元测试

c# - 将 C# 动态对象序列化为 JSON 对象以供 javascript 使用

c# - 在具有字符串列值的数据表中搜索整数项

c# - AutoMapper - 如何在使用 AutoMapper.Map 时忽略源上的空字段 - 有时

c# - 如何使用 AutoMapper 根据展平属性的名称查找源属性