Good morning. I need to update the mapping of items in an order based on the item(s) being "invoiced". Currently, there is no conditional mapping of order items. I've been looking at conditional and preconditional mapping in Automapper, but I'm not sure how to do this for a List of items.
Here's what it looks like currently.
CreateMap<OrderDto, Order>()
.ForMember(dest => dest.TransactionId, opt => opt.MapFrom(src => CreateTransactionId(src.OrderHeader)))
.ForMember(dest => dest.TransactionType, opt => opt.MapFrom(src => MapTransactionType(src.OrderHeader.OrderType)))
.ForMember(dest => dest.OrderItems, opt => opt.MapFrom(src => src.OrderLine ) )
.ReverseMap();
CreateMap<OrderLineDto, OrderItem>()
.ForMember(dest => dest.LineNumber, opt => opt.MapFrom(src => src.LineNumber))
.ForMember(dest => dest.Upc, opt => opt.MapFrom(src => src.Item.Upc))
.ForMember(dest => dest.Sku, opt => opt.MapFrom(src => src.Item.Upc))
.ReverseMap();
So, the logic I need to add is to only map OrderLines that have an entry in the orderLines.quantity(list).type='Invoiced'. I'm sure this is probably a simple process, I just couldn't find an example. Thank you for recommendations!
You can use LINQ
to filter the type to be "Invoiced" by:
.ForMember(dest => dest.OrderItems, opt => opt.MapFrom(src => src.OrderLine.Where(t => t.Quantity.FirstOrDefault().Type == "Invoiced")))