【发布时间】:2022-01-13 16:47:42
【问题描述】:
我正在使用自动映射器来映射源类和目标类,如下所示
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<SourceClass, DestinationClass>()
.ForMember(dest => dest.P1, act => act.MapFrom(src => src.S1))
.ForMember(dest => dest.P2, act => act.MapFrom(src => src.S2))
.ForMember(dest => dest.P3, act => act.MapFrom(src => src.S3));
});
IMapper mapper = config.CreateMapper();
DestinationClass destObject= mapper.Map<DestinationClass>(sourceObj);
这里可以将单个对象映射到列表吗?喜欢cfg.CreateMap<SourceClass, List<DestinationClass>>()。
映射后,我期望目标类列表具有源类的值。这意味着一个包含一个元素的列表。是否可以使用 .net core 将单个对象映射到列表?
【问题讨论】:
-
那不编译。您有几个分号放错了位置。
-
@PalleDue ,更新了相关代码
-
@YongShun 我们可以不用自定义转换器来做到这一点
-
您好,如果没有自定义转换器,您必须将单个
SourceClass对象添加到List<SourceClass>数组。然后使用数组映射到List<DestinationClass>数组。
标签: c# asp.net-mvc asp.net-core asp.net-web-api automapper