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; }
}
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.
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"
}
}
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?
I would say: this is a bug: see Github Issue 159.
The AutoMapper.QueryableExtensions uses
Mapper.CreateMapExpressioninternally so if you write:It will also fail with the same exception.
It seems
Mapper.CreateMapExpressioncurrently does not support:List<string>etc.string[],Bar[]etc.But if you make your
FootoList<Item>it can work: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)