Reputation: 8467
I can get Automapper's .Map
to return the derived type, but not in an IQueryable
.
Data structure:
public class Order { }
public class OnlineOrder : Order { }
public class MailOrder : Order { }
public class OrderDto { }
public class OnlineOrderDto : OrderDto { }
public class MailOrderDto : OrderDto { }
Setup (~copied from documentation):
var mapperConfiguration = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Order, OrderDto>()
.Include<OnlineOrder, OnlineOrderDto>()
.Include<MailOrder, MailOrderDto>();
cfg.CreateMap<OnlineOrder, OnlineOrderDto>();
cfg.CreateMap<MailOrder, MailOrderDto>();
});
var order = new OnlineOrder();
var orders = new List<Order> { order }.AsQueryable();
var mapper = mapperConfiguration.CreateMapper().Map<OrderDto>(order); // is of type OnlineOrderDto, it works!
What I've tried:
var projectTo = orders.ProjectTo<OrderDto>(mapperConfiguration); // is of type OrderDto, boo!
var useAsDataSource = orders.UseAsDataSource(mapperConfiguration).For<OrderDto>(); // is of type OrderDto, boo!
I've looked at documentation, this answer, the queryable extensions, and some answers involving LINQ. I feel like I'm missing something obvious.
Upvotes: 2
Views: 141
Reputation: 1725
I don't think it is possible. See this closed bug report: https://github.com/AutoMapper/AutoMapper/issues/701
Upvotes: 2