AutoMapping Array to a List

≯℡__Kan透↙ 提交于 2019-12-24 00:47:19

问题


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:


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());



回答2:


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()))


来源:https://stackoverflow.com/questions/24539606/automapping-array-to-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!