Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

432
Vistas
how multiple map in automapper

I recently met with an automapper. I have a question, how i can map multiple objects from my repostiory in one DTO.

public QuestionProfile()
{
    CreateMap<Question, GrammarQuestionDTO>()
        .ForMember(g => g.Question, q => q.MapFrom(source => source.Text)).ReverseMap();

    CreateMap<List<GrammarQuestionDTO>, TestDTO>()
        .ForMember(t => t.GrammarQuestion, g => g.MapFrom(source => source)).ReverseMap();

    CreateMap<Question, AuditionQuestionDTO>()
        .ForMember(a => a.Question, q => q.MapFrom(source => source.Text))
        .ForMember(a => a.Url, q => q.MapFrom(source => source.AudioFile.Url)).ReverseMap();

    CreateMap<List<AuditionQuestionDTO>, TestDTO>()
        .ForMember(t => t.AuditionQuestion, a => a.MapFrom(source => source)).ReverseMap();
}

how can I map all these 4 objects?

public async Task<TestDTO> GenerateTest(LevelType level)
{
    var grammarQuestions = await _questionRepository.GetGrammarQuestionAsync(level);
    var auditionQuestions = await _questionRepository.GetAuditionQuestionAsync(level);
    var essayTopic = await _questionRepository.GetEssayTopicAsync(level);
    var speakingTopic = await _questionRepository.GetSpeakingTopicAsync(level);

    var test = _mapper.Map<TestDTO>(grammarQuestions);
    test = _mapper.Map(test, auditionQuestions); //there will be a conversion error

    return _mapper.Map<TestDTO>(grammarQuestions);
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You need to map it without list, just map it one to one and AutoMapper will handle the collections:

CreateMap<AuditionQuestionDTO, TestDTO>()
    .ForMember(t => t.AuditionQuestion,
               a => a.MapFrom(source => source.<PropertyName>)).ReverseMap();

And then on the map you should:

test = _mapper.Map<List<AuditionQuestionDTO>>(test, auditionQuestions);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda