简体   繁体   中英

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). The problem is that my DBObject contains a dictionary where the values are the property that I've like to map into a list.

eg. dBObject.CustomProperties.Values --> dtoObject.CustomProperties

Can this be acheieved through 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)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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