当投影IQueryable< object>时,自动映射器失败.当对象具有集合属性时 [英] Automapper fails when projecting IQueryable<object> when object has a collection property

查看:71
本文介绍了当投影IQueryable< object>时,自动映射器失败.当对象具有集合属性时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这就是我的情况.我有两节课:

So, here's my situation. I've got a my two classes:

class FromClass
{
    public string[] Foo { get; set; }
}

class ToClass
{
    public string[] Foo { get; set; }
}

这些类具有作为数组的属性.它们可能是List<T>IEnumerable<T>,在任何情况下我都得到相同的结果.

The classes have properties which are arrays. They could be List<T> or IEnumerable<T>, I get the same result in any of those cases.

我尝试使用AutoMapper.QueryableExtensions从一个映射到另一个:

I try to map from one to the other using the AutoMapper.QueryableExtensions:

class Program
{
    static void Main(string[] args)
    {
        // create a "From" object
        string[] anArray = new string[] { "a", "b" };
        FromClass anObject = new FromClass() { Foo = anArray };

        // make a queryable set that includes the "From" object
        IQueryable<FromClass> queryableObjects = (new FromClass[] { anObject }).AsQueryable();

        // set up AutoMapper
        Mapper.CreateMap<FromClass, ToClass>();
        Mapper.AssertConfigurationIsValid();

        // test plain mapping
        IQueryable<ToClass> test1 = queryableObjects.Select(o => Mapper.Map<FromClass, ToClass>(o));
            // success!

        // test queryable extensions
        IQueryable<ToClass> test2 = queryableObjects.Project().To<ToClass>();
            // InvalidOperationException: "Sequence contains no elements"

    }
}

为什么test2抛出InvalidOperationException?如果我将Foo的类型设为非集合,例如string或其他类-则一切正常.

Why does test2 throw an InvalidOperationException? If I make the type of Foo something that's not a collection, e.g. a string or some other class -- then everything works perfectly.

我做错什么了吗?不懂事吗?还是我遇到了错误?

Am I doing something wrong? Not understanding something? Or have I hit a bug?

推荐答案

我会说:这是一个错误:请参阅 Github问题159 .

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

AutoMapper.QueryableExtensions在内部使用Mapper.CreateMapExpression,因此如果您编写:

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.

似乎Mapper.CreateMapExpression当前不支持:

  • 值类型的通用集合,例如List<string>
  • 复杂类型或值类型的数组,例如string[]Bar[]等.
  • generic collections of value types e.g. List<string> etc.
  • arrays of complex types or value types e.g string[], Bar[] etc.

但是,如果您将Foo设置为List<Item>,则可以使用:

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)

这篇关于当投影IQueryable&lt; object&gt;时,自动映射器失败.当对象具有集合属性时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆