Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda