[英]How to Map Dictionary<int, Product> and List<ProductDto> using automapper?
我有這個代碼,想知道如何為此創建地圖配置
public Product{ int ProductId, string ProductName}
public ProductDto { int ProductId, string ProductName}
_mapper.Map<Dictionary<int, Product>, List<ProductDto >>(product);
現在這就是我所擁有的,並且正在修補 LINQ 來解決這個問題。
public MappingProfile()
{
CreateMap<Dictionary<int, Product>, List<ProductDto>>()
.ForMember(dest => new List(){ new ProductDto(){}}, opt => opt.MapFrom(src => src.Values.))
}
在這種情況下Dictionary<int, Product>
您不需要創建從Dictionary<int, Product>
到List<ProductDto>
的映射配置文件。 Dictionary<K, V>
是一個IEnumerable<KeyValuePair<K, V>>
所以如果你配置從KeyValuePair<int, Product>
到ProductDto
的轉換,AutoMapper 將處理剩下的。
CreateMap<Product, ProductDto>();
CreateMap<KeyValuePair<int, Product>, ProductDto>()
.ConstructUsing((pair, context) => context.Mapper.Map<Product, ProductDto>(pair.Value));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.