Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapping Array to a List

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

like image 445
Proximo Avatar asked Oct 24 '25 17:10

Proximo


1 Answers

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());
like image 188
Justin Niessner Avatar answered Oct 26 '25 07:10

Justin Niessner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!