4
class A
{
  public List<string> list;
}

class B
{
  public string[] array;
}

How would you map this?

I've tried

CreateMap<A,B>();

That doesn't work

1
  • b.array = a.list.ToArray() ? Commented Jul 2, 2014 at 19:36

2 Answers 2

5

Your first issue is going to be that the class members don't match. If they did, I'd imagine that this would work. If not, you just have to specify your mapping rather than letting Automapper infer it:

CreateMap<A,B>()
    .ForMember(d => d.array, opts => opts.MapFrom(s => s.list.ToArray());
Sign up to request clarification or add additional context in comments.

3 Comments

The collection is not a string but a NameValueCollection for two different name spaces so I'm assuming those need to be mapped too. Any ideas there?
Yes, it's because the names don't match, it has nothing to do with arrays/lists. @Proximo you might want to update your question to include the actual collection types.
I was going to but I figured out how to map the different internal collections individually. This answer saved the day.
0

For a shortcut, a vb.net version

CreateMap(Of A, B)().ForMember(Function(d) d.array, Sub(opts) opts.MapFrom(Function(s) s.list.ToArray()))

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.