c# Automapper在嵌套列表上



我有以下dtos,我正在尝试使用automapper,但不能完全理解如何做同样的,因为这是我第一次使用automapper任何帮助真的很感激

string order {get;set;}
string orderType {get;set;}
List<OrderItem> Items {get;set;}
}
public class OrderItem{
string itemID {get;set;}
Price price {get;set;}
}
public class Price {
string total {get;set;}
string regular {get;set;}
}
public class request {
string order {get;set;}
string orderType {get;set;}
List<Item> Items {get;set;}
}
public class Item {
string itemID {get;set;}
string itemPrice {get;set;}
string regularPrice {get;set;}
}

I'm trying the following
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<request,requestDTO>()
.ForMember(dest => dest.order, act=>act.MapFrom(src => src.order))
.ForMember(dest => dest.orderType, act=>act.MapFrom(src => src.orderType))
})
How do I create mapping to map 
**
src.item.itemID to dest.item.itemID
src.item.itemPrice to dest.item.price.total
src.item.regularPrice to dest.item.price.regular**
for all the items in my source array

ItemOrderItem创建另一个地图,如下所示:

cfg.CreateMap<Item, OrderItem>()
.ForPath(dest => dest.price.total, act => act.MapFrom(src => src.itemPrice))
.ForPath(dest => dest.price.regular, act =>act.MapFrom(src => src.item.regularPrice));

还注意映射器<request,requestDTO>不需要任何显式的ForMember调用,因为两个对象具有相同的属性名称:

cfg.CreateMap<request,requestDTO>();

相关内容

  • 没有找到相关文章

最新更新