簡體   English   中英

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

[英]Map a dictionary's values to a list using 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)));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM