Match one object to one of three objects
Please see the code below that I took from here: https://github.com/AutoMapper/AutoMapper/wiki/Mapping-inheritance (section Runtime Politics)
public class Order { }
public class OnlineOrder : Order { }
public class MailOrder : Order { }
public class OrderDto { }
public class OnlineOrderDto : OrderDto { }
public class MailOrderDto : OrderDto { }
Mapper.Initialize(cfg => {
cfg.CreateMap<Order, OrderDto>()
.Include<OnlineOrder, OnlineOrderDto>()
.Include<MailOrder, MailOrderDto>();
cfg.CreateMap<OnlineOrder, OnlineOrderDto>();
cfg.CreateMap<MailOrder, MailOrderDto>();
});
// Perform Mapping
var order = new OnlineOrder();
var mapped = Mapper.Map(order, order.GetType(), typeof(OrderDto));
Assert.IsType<OnlineOrderDto>(mapped);
Works as expected. Let's say I changed my code like this:
https://github.com/AutoMapper/AutoMapper/wiki/Mapping-inheritance
public class Order {
public class OnlineOrder : Order { int type;}}
public class OrderDto { }
public class OnlineOrderDto : OrderDto { }
public class MailOrderDto : OrderDto { }
Let's say I wanted to map an OnlineOrder to one of the DTOs following this logic:
if (OnlineOrder.Type==1) { return new OnlineOrderDto() }
elseif (OnlineOrder.Type==2) { return new MailOrderDto() }
I'm actually trying to do this in LINQ for Entities:
list = CreditCardPreQualificationDatabase .dbOffers .Select(Mapper.Map<OrderDTO>) .ToList();
After that it will start; the list should contain the types: OnlineOrderDto and MailOrderDto
Can this be done?
+3
source to share
No one has answered this question yet
Check out similar questions: