33

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? Commented Oct 26, 2009 at 19:58
  • Just unable to map between an array to an ICollection<T>. Mapping exception is thrown. Commented Oct 27, 2009 at 12:11

2 Answers 2

59

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

Sign up to request clarification or add additional context in comments.

2 Comments

What about if I want to map a string[] to a Y? e.g. where each string in the array will map to a different property of Y?
Link to automaper is broken
4

Now it looks like you can use:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.