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

标签 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 的答案。我觉得我错过了一些明显的东西。

最佳答案

我认为这是不可能的。请参阅此已关闭的错误报告: https://github.com/AutoMapper/AutoMapper/issues/701

关于c# - 当输入为 IQueryable 时,无法让 Automapper 返回派生类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44832595/

相关文章:

c# - 将 IQueryable where 子句从 DTO 映射到实体

c# - linq to sql - 我如何重写以下内容

c# - 如何在 WPF 中模拟悬挂电缆?

c++ - 代码无法按预期使用虚函数和继承

delphi - 如何定义其参数在后代类中不同的虚函数?

c# - AutoMapper 花费 4 秒绘制 19 个对象

c# - 带外键的 Fluent nHibernate "not-null property references a null or transient value"

c# - Linq 的 Enumerable.Count 方法检查 ICollection<> 但不检查 IReadOnlyCollection<>

C++ 多态错误

c# - Automapper ProjectTo 将 ToList 添加到子属性中