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

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

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