Using Automapper 3.3.1.0 there is a different mapping behavior between the usage of Mapper.Map<IEnumerable<TDestination>>(someEnumerable)
compared to someEnumerable.AsQueryable().Project().To<TDestination>()
This does not appear to be a limitation of a SQL LINQ provider or other as this is witnessed in an in-memory collection.
As with many things this is best explained by example:
Note: the following code can be found at 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?");
}
}
}
I understand there are limitations to the .Project().To<TDestination>
IQueryable extensions but what I am unaware of is:
- which limitation is causing this behavior?
- is this a Automapper limitation or a LINQ limitation
- is there a work around to still use the queryable extensions and not revert
Mapper.Map<TDestination>(obj)
exclusively?
for posterity: link discussion thread topic
This is a LINQ limitation. AutoMapper does not inherit base mappings for LINQ. What would be the Select expression to do polymorphic Select projections? Trying to do that leads you to where you can't do this. LINQ query providers don't support it.