0

I am trying to map a source list to a destination array using AutoMapper.

Source classes

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; }
}

Destination classes

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

Map configuration

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

This is what I have tried, here I would like to eliminate the GetRoutes method where I will do a manual map using a foreach loop. Is it possible to use any other way without a loop?

1 Answer 1

1

Add mapping for Trip and Route classes.

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

Complete Mapping Configuration

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

Output

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

References

Lists and Arrays - AutoMapper documentation

Sign up to request clarification or add additional context in comments.

6 Comments

Missing type map configuration or unsupported mapping.\r\n\r\nMapping types:\r\nClient -> partners[] .... i get this error , here the field is not specified and ll the properties are string on both source and destination
Hmmm, I don't see any partners[] from your models. Perhaps try to provide a minimal, reproducible example so I can check on it. Thanks.
dotnetfiddle.net/bzp9H7 @Yung Shan I have added the sample code here ..I am trying to eliminate the manual mapping here.
I have added the sample code here
Hi, sorry for being late. I see there are quite a lot of changes in code compared to the previous code. The answer is written based on Post Owner's provided question. You can edit the question to specify and provide more detail for the question within the scope. Now your question is more like another question that is not relevant to the current question. I will suggest you post another question. Thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.