Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
How to map a flat model to a Class that has a string property and IEnumerable<CustomClass> using automapper

I am new to automapper and am wondering how I could map a flat model into a Dto with 1 string property and IEnumerable

I have these classes

And I want to map this Model

public class DirectionModel
{
    public string DirectionId { get; set; }
    public decimal Longitude { get; set; }
    public decimal Latitude { get; set; }
}

To this DTO classes

public class DirectionDto
{
    public string DirectionId { get; set; }
    public IEnumerable<CoordinateDto> Coordinates { get; set; }
}

public class CoordinateDto
{
    public decimal Longitude { get; set; }
    public decimal Latitude { get; set; }
}

Sample Input:

IEnum<DirectionModel>()
{
    new DirectionModel() {DirectionId="id1", Longitude=1, Latitude=2},
    new DirectionModel() {DirectionId="id1", Longitude=3, Latitude=4},
    new DirectionModel() {DirectionId="id2", Longitude=5, Latitude=6}
}

This is the expecteed output:

IEnum<DirectionDto>()
{
    new DirectionDto() {DirectionId="id1", IEnum<CoordinateDto>(){new CoordinateDto(){Longitude=1, Latitude=2}, new CoordinateDto(){Longitude=3, Latitude=4}}},
    new DirectionDto() {DirectionId="id2", IEnum<CoordinateDto>(){new CoordinateDto(){Longitude=5, Latitude=6}}},
}

(edit: Corrected the sample input which may have caused confusion)

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use AfterMap in AutoMapper, I prefer to use List instead of IEnumerable like this:

autoMapperConfig.CreateMap<List<DirectionModel>, List<DirectionDto>>()
                            .AfterMap((model, dto) =>
                            {
                                if (dto == null)
                                    dto = new List<DirectionDto>();

                                if (model.Any())
                                {
                                    dto.AddRange(model.GroupBy(x => x.DirectionId)
                                        .Select(x => new DirectionDto()
                                        {
                                            DirectionId = x.Key,
                                            Coordinates = x.ToList()
                                                .Select(c => new CoordinateDto()
                                                {
                                                    Latitude = c.Latitude,
                                                    Longitude = c.Longitude
                                                }).ToList()
                                        }).ToList());
                                }
                            });

And use it:

   var lists = new List<DirectionModel>()
            {
                new DirectionModel() {DirectionId = "id1", Longitude = 1, Latitude = 2},
                new DirectionModel() {DirectionId = "id1", Longitude = 3, Latitude = 4},
                new DirectionModel() {DirectionId = "id2", Longitude = 5, Latitude = 6}
            };

    var dtos = _mapper.Map<List<DirectionModel>, List<DirectionDto>>(lists);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!