当输入为IQueryable时,无法让Automapper返回派生类型

时间:2017-06-29 18:34:06

标签: c# polymorphism automapper iqueryable

我可以让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,此answerqueryable extensions以及一些涉及LINQ的答案。我觉得我错过了一些明显的东西。

1 个答案:

答案 0 :(得分:2)

我认为不可能。看到这个封闭的错误报告: https://github.com/AutoMapper/AutoMapper/issues/701