自动将数组映射到列表 [英] AutoMapping Array to a List

查看:75
本文介绍了自动将数组映射到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class A
{
  public List<string> list;
}

class B
{
  public string[] array;
}

您将如何映射?

我尝试过

CreateMap<A,B>();

那行不通

推荐答案

您的第一个问题将是类成员不匹配.如果他们做到了,我想这会奏效.如果没有,您只需要指定映射即可,而不必让Automapper进行推断:

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

这篇关于自动将数组映射到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆