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

434
Views
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 answers
Answer question

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