Brian Liang
Brian Liang

Reputation: 7774

Mapping collections using AutoMapper

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?

Upvotes: 33

Views: 38598

Answers (2)

tmgirvin
tmgirvin

Reputation: 1497

Now it looks like you can use:

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

Upvotes: 4

Drew Freyling
Drew Freyling

Reputation: 1258

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

Upvotes: 59

Related Questions