简体   繁体   English

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

[英]Map a dictionary's values to a list using AutoMapper

I have a Database Object which has a dictionary of properties which I need to map to a list object within a new object. 我有一个数据库对象,该对象具有属性字典,我需要将其映射到新对象中的列表对象。 My structure (simplified) looks like this: 我的结构(简化)如下所示:

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

As you can see, I would like to map DbObject (the source) onto a DTOObject (the destination). 如您所见,我想将DbObject(源)映射到DTOObject(目标)上。 The problem is that my DBObject contains a dictionary where the values are the property that I've like to map into a list. 问题是我的DBObject包含一个字典,其中的值是我想映射到列表中的属性。

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

Can this be acheieved through AutoMapper? 可以通过AutoMapper实现吗?

You can use: 您可以使用:

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

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

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