IQueryable 扩展行为因自动映射器多态集合而异



使用自动映射器 3.3.1.0 时,与someEnumerable.AsQueryable().Project().To<TDestination>()相比,Mapper.Map<IEnumerable<TDestination>>(someEnumerable) 的使用之间的映射行为不同

这似乎不是 SQL LINQ 提供程序或其他方面的限制,因为在内存中集合中可以看到这种情况。

与许多事情一样,最好通过示例来解释:

注意: 以下代码可以在 https://gist.github.com/kmoormann/b3949d006f4083ab6ee4

using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FluentAssertions;
using NUnit.Framework;
namespace Automapper.PolymorphicList.Tests
{
    [TestFixture]
    class AutomapperQueryableExtensionPolymorphism
    {
        //taking the class structure from: https://github.com/AutoMapper/AutoMapper/wiki/Mapping-inheritance
        public class Order { }
        public class OnlineOrder : Order
        {
            public string Referrer { get; set; }
        }
        public class MailOrder : Order { }
        //Dtos
        public class OrderDto
        {
            public string Referrer { get; set; }
        }
        [Test(Description = "Does the same mapping behavior exist for a polymorphic list when doing the project querable extension as when doing the static mapper map method()")]
        public void IsSameBehaviorForQueryableExtensionAndStaticMap()
        {
            Mapper.Reset();
            //Mappings
            Mapper.CreateMap<Order, OrderDto>()
                .Include<OnlineOrder, OrderDto>()
                .Include<MailOrder, OrderDto>()
                .ForMember(o => o.Referrer, m => m.Ignore());
            Mapper.CreateMap<OnlineOrder, OrderDto>();
            Mapper.CreateMap<MailOrder, OrderDto>();
            //build lists
            var onlineOrders = new List<OnlineOrder>() { new OnlineOrder() { Referrer = "one" }, new OnlineOrder() { Referrer = "two" } };
            var mailOrders = new List<MailOrder>() { new MailOrder() };
            //single typed list mapping
            var mappedOnlineOrderDtos = Mapper.Map<IEnumerable<OrderDto>>(onlineOrders);
            var projectedOnlineOrderDtos = onlineOrders.AsQueryable().Project().To<OrderDto>();
            //using FluentAssertions for collection assertions
            projectedOnlineOrderDtos.ShouldBeEquivalentTo(mappedOnlineOrderDtos, "automapper can handle singly typed lists");

            //other single typed list mapping
            var mappedMailOrderDtos = Mapper.Map<IEnumerable<OrderDto>>(mailOrders);
            var projectedMailOrderDtos = mailOrders.AsQueryable().Project().To<OrderDto>();
            projectedMailOrderDtos.ShouldBeEquivalentTo(mappedMailOrderDtos, "automapper can handle singly typed lists");
            //build a polymorphic list
            var orders = new List<Order>();
            orders.AddRange(onlineOrders);
            orders.AddRange(mailOrders);
            // Perform Mapping and Projection
            var mappedPolymorhpicOrders = Mapper.Map<IEnumerable<OrderDto>>(orders);
            var projectedPolymorphicOrders = orders.AsQueryable().Project().To<OrderDto>();
            projectedPolymorphicOrders.ShouldBeEquivalentTo(mappedPolymorhpicOrders, "automapper can handle polymorphic typed lists?");
        }
    }
}

我知道 IQueryable 扩展.Project().To<TDestination>有局限性,但我不知道的是:

  • 哪个限制导致此行为?
  • 这是自动映射器限制还是 LINQ 限制
  • 是否有解决方法仍然可以使用可查询的扩展而不以独占方式还原Mapper.Map<TDestination>(obj)

对于后代:链接讨论线程主题

这是一个 LINQ 限制。自动映射器不继承 LINQ 的基本映射。用于执行多态选择投影的选择表达式是什么?试图这样做会导致你无法做到这一点。LINQ 查询提供程序不支持它。

相关内容

  • 没有找到相关文章

最新更新