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

4

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

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字典展平]的重复问题(https://dev59.com/LmPVa4cB1Zd3GeqP7qVb)。 - Alexandru Marculescu
1个回答

4

您可以使用:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接