32
votes

I'm trying to map an array into an ICollection of type <T>.

Basically I want to be able to do:

Mapper.CreateMap<X[], Y>();

Where Y is Collection<T>

Any ideas?

2
What's the behavior you're seeing right now?Jimmy Bogard
Just unable to map between an array to an ICollection<T>. Mapping exception is thrown.Brian Liang

2 Answers

57
votes

You don't need to setup your mapping for collections, just the element types. So just:

Mapper.CreateMap<X, Y>();
Mapper.Map<X[], Collection<Y>>(objectToMap);

See here for more info: http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays&referringTitle=Home

2
votes

Now it looks like you can use:

Mapper.CreateMap<X,Y>(); 
var listOfX = Mapper.Map<List<X>>(someIEnumerableOfY);