AutoMapper - 将源列表映射到目标数组

Posted

技术标签:

【中文标题】AutoMapper - 将源列表映射到目标数组【英文标题】:AutoMapper - Map source list to a destination array 【发布时间】:2022-01-02 19:09:56 【问题描述】:

我正在尝试使用 AutoMapper 将源列表映射到目标数组。

源类

public class ReservationSource

    public Travel TravelSource  get; set; 

    public string SeqNo  get; set; 


public class Travel

    public string TravelId  get; set; 

    public ICollection<Trip> Trips  get; set; 


public class Trip

    public string  TrainNumber  get; set; 
    public string  Arrival  get; set; 

目标类

public class ReservationDestination 

    public Route[] TravelDest  get; set; 
    public string SeqNumber  get; set; 


public class Route

    public string SequNumber  get; set; 
    public string RouteId  get; set; 


private static route[] GetRoutes(ICollection<Trip> trips)

    List<route> routeList = new List<route>();

    foreach (var trip trips)
    
        var route = new route
        
            SequNumber = trip.trainNumber

        ;
        routeList.Add(route);
    
    return routeList.ToArray();

地图配置

cfg.CreateMap<ReservationSource, ReservationDestination>()

var config = new MapperConfiguration(cfg =>

    cfg.AllowNullDestinationValues = true;
    cfg.CreateMap<ReservationSource, ReservationDestination>()
        .ForMember(dest => dest.SeqNumber, o => o.MapFrom(src => SeqNo))
        .ForPath(dest => dest.TravelDest, o => o.MapFrom(src => GetRoutes(src)));
);

这是我尝试过的,在这里我想消除GetRoutes 方法,我将使用foreach 循环进行手动映射。是否可以在没有循环的情况下使用任何其他方式?

【问题讨论】:

【参考方案1】:

TripRoute 类添加映射。

cfg.CreateMap<Trip, Route>()
    .ForMember(dest => dest.SequNumber, o => o.MapFrom(src => src.TrainNumber));

完整的映射配置

var config = new MapperConfiguration(cfg =>
       
    cfg.CreateMap<Trip, Route>()
        .ForMember(dest => dest.SequNumber, o => o.MapFrom(src => src.TrainNumber));
            
    cfg.AllowNullDestinationValues = true;
    cfg.CreateMap<ReservationSource, ReservationDestination>()
        .ForMember(dest => dest.SeqNumber, o => o.MapFrom(src => src.SeqNo))
        .ForPath(dest => dest.TravelDest, o => o.MapFrom(src => src.TravelSource.Trips));
);

IMapper mapper = config.CreateMapper();
        
var source = new ReservationSource

    SeqNo = "Seq001",
    TravelSource = new Travel
    
        TravelId = "1",
        Trips = new List<Trip>
        
            new Trip  TrainNumber = "A0001" ,
            new Trip  TrainNumber = "B0001" 
        
    
;
        
var destination = mapper.Map<ReservationSource, ReservationDestination>(source);
Console.WriteLine(JsonConvert.SerializeObject(destination));

Sample Program

输出

"TravelDest":["SequNumber":"A0001","RouteId":null,"SequNumber":"B0001","RouteId":null],"SeqNumber":"Seq001"

参考文献

Lists and Arrays - AutoMapper documentation

【讨论】:

缺少类型映射配置或不支持的映射。\r\n\r\n映射类型:\r\nClient -> partners[] .... 我收到此错误,此处未指定字段并且所有属性在源和目标上都是字符串 嗯,我在您的模型中没有看到任何partners[]。也许尝试提供一个最小的、可重复的示例,以便我检查它。谢谢。 dotnetfiddle.net/bzp9H7@Yung Shan 我已经在这里添加了示例代码..我正在尝试消除这里的手动映射。 我这里添加了示例代码 嗨,很抱歉迟到了。我看到与以前的代码相比,代码有很多变化。答案是根据帖子所有者提供的问题编写的。您可以编辑问题以在范围内指定问题并提供更多详细信息。现在您的问题更像是另一个与当前问题无关的问题。我会建议你发布另一个问题。谢谢。

以上是关于AutoMapper - 将源列表映射到目标数组的主要内容,如果未能解决你的问题,请参考以下文章

AutoMapper用法

AutoMapper用法

AutoMapper用法

AutoMapper用法

AutoMapper实际项目运用

AutoMapper C#实体映射