小编DCA*_*TEK的帖子

Automapper 将 int 属性值从 List<type> 映射到 List<int>

我需要将对象列表中的 int 属性映射到List<int>. 这是我的类结构的样子:

我有一个父类:

public class Parent
{
    ...
    public List<Location> Locations { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

位置类:

public class Location
{
    public int LocationId { get; set; }
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

映射的目标类:

public class Destination
{
    ...
    public List<int> Locations { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是我试图用来完成List<Location>to之间映射的代码List<int>

CreateMap<Parent, Destination>()
.ForMember(d => d.Locations, o => o.MapFrom(s => s.Locations.Select(l => l.LocationId)))
Run Code Online (Sandbox Code Playgroud)

这不起作用。我收到以下错误:

AutoMapper.AutoMapperMappingException:无法创建从Location.LocationId (System.Collections.Generic.IEnumerable`1[System.Int32])Destination.Locations (System.Collections.Generic.List`1[System.Int32])的地图表达式

知道我做错了什么吗?

c# automapper

3
推荐指数
2
解决办法
2404
查看次数

标签 统计

automapper ×1

c# ×1