How to use ProjectTo with Automapper 8.0 Dependency injection

Starting with 8.0 you can use IMapper.ProjectTo

This means that now IMapper interface has a method ProjectTo (similar to Map). So while you still need injecting IMapper (but you need it anyway if you were using Map, so no difference), you don't need QueryableExtensions and ProjectTo extension method - you simply use the interface method (similar to Map):

return await _mapper.ProjectTo<SomeViewModel>(dbContext.SomeEntity)
    .ToListAsync();

Please note that there is fundamental difference between _mapper.ProjectTo and Select(_mapper.Map) - the former is translated to SQL and executed server side, while the latter leads to client evaluation and needs Include / ThenInclude (or lazy loading) in order to function properly.


I had the same issue, and this worked for me:

var teacher = _mapper.ProjectTo<TeacherReadDto>(_applicationDb.Teachers.Where(x=>x.Id==id));