我可以让Automapper的.Map
返回派生类型,但不能返回IQueryable
。
数据结构:
public class Order { }
public class OnlineOrder : Order { }
public class MailOrder : Order { }
public class OrderDto { }
public class OnlineOrderDto : OrderDto { }
public class MailOrderDto : OrderDto { }
设置(〜复制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!
我尝试了什么:
var projectTo = orders.ProjectTo<OrderDto>(mapperConfiguration); // is of type OrderDto, boo!
var useAsDataSource = orders.UseAsDataSource(mapperConfiguration).For<OrderDto>(); // is of type OrderDto, boo!
我查看了documentation,此answer,queryable extensions以及一些涉及LINQ的答案。我觉得我错过了一些明显的东西。