Automapper fails when projecting IQueryable<object> when object has a collection property

为君一笑 提交于 2019-12-03 09:06:48

I would say: this is a bug: see Github Issue 159.

The AutoMapper.QueryableExtensions uses Mapper.CreateMapExpression internally so if you write:

var expression = Mapper.CreateMapExpression<FromClass, ToClass>();

It will also fail with the same exception.

It seems Mapper.CreateMapExpression currently does not support:

  • generic collections of value types e.g. List<string> etc.
  • arrays of complex types or value types e.g string[], Bar[] etc.

But if you make your Foo to List<Item> it can work:

public class FromClass
{
    public List<Item> Foo { get; set; }
}

public class ToClass
{
    public List<Item> Foo { get; set; }
}

public class Item
{
    public string Bar { get; set; }
}

var list =  new List<Item> { new Item{ Bar = "a"}, new Item() { Bar= "b" }};
FromClass anObject = new FromClass() { Foo = list };
var queryableObjects = (new FromClass[] { anObject }).AsQueryable();
Mapper.CreateMap<FromClass, ToClass>();
Mapper.CreateMap<Item, Item>();
var test2 = queryableObjects.Project().To<ToClass>().ToArray();

You can comment on the above mentioned issue or create a new one with your code (it's a quite good repro of the bug)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!