简体   繁体   中英

Automapper for inner join LINQ

Currently, I have successfully use Automapper to map my hall entity to Dto as below:

    [HttpGet]
    public async Task<ActionResult<IEnumerable<HallDto>>> GetHallDto()
    {
        return await _context.Halls
            .ProjectTo<HallDto>(_mapper.ConfigurationProvider)
            .ToListAsync();
    }

But for another web api method below, how do I use Automapper when my I have inner join LINQ? At the moment, this is how I manually map them.

    [HttpGet]
    public async Task<ActionResult<IEnumerable<MovieHallDto>>> GetMovieHallDto()
    {
        var movieHallListDto = await (from mh in _context.MovieHalls
                             join m in _context.Movies on mh.MovieId equals m.Id
                             join h in _context.Halls on mh.HallId equals h.Id
                             select new MovieHallDto
                             {
                                 Id = mh.Id,
                                 MovieTitle = m.MovieTitle,
                                 HallNo = h.HallNo,
                                 MovieDateTime = mh.MovieDateTime
                             }).ToListAsync();

        return movieHallListDto;
    }

Here is my AutoMapperProfile

public class AutoMapperProfile : Profile
{
    public AutoMapperProfile()
    {
        CreateMap<Movie, MovieDto>();
        CreateMap<Hall, HallDto>();
        CreateMap<MovieHall, MovieHallDto>();
    }
}

It doesn't matter how many joins you have to get your data from the database. It is easy to map the required properties as shown below.

In your case, I think you wanted to know how to map List. You should configure the Mapper correctly as shown below.

 CreateMap<MovieHallDto, MovieHall>(); //<Source, Destination>
// MovieHall class should have Id , MovieTitle ,HallNo , MovieDateTime  properties

Modify your output of the API.

[HttpGet]
    public async Task<ActionResult<IEnumerable<MovieHall>>> GetMovieHallDto()
    {
        var movieHallListDto = await (from mh in _context.MovieHalls
                             join m in _context.Movies on mh.MovieId equals m.Id
                             join h in _context.Halls on mh.HallId equals h.Id
                             select new MovieHallDto
                             {
                                 Id = mh.Id,
                                 MovieTitle = m.MovieTitle,
                                 HallNo = h.HallNo,
                                 MovieDateTime = mh.MovieDateTime
                             }).ToListAsync();

var model = Mapper.Map<List<MovieHall>>(movieHallListDto );

return Ok(model );

    }

If you have the tables with proper referential integrity, you can use like below.

[HttpGet]
public async Task<ActionResult<IEnumerable<MovieHall>>> GetMovieHallDto()
{

    var movieHalls  = _context.MovieHalls.Include(c => c.Movies).ThenInclude(c => c.Halls).ToListAsync();

    var model = Mapper.Map<List<MovieHall>>(movieHalls);

    return Ok(model);

}
CreateMap<MovieHallDto, MovieHall>(MemberList.None)
    .ForMember(d => d.Id , s => s.MapFrom(des => des.Id))
.ForMember(d => d.MovieTitle , s => s.MapFrom(des => des.Movies.MovieTitle))
.ForMember(d => d.HallNo  , s => s.MapFrom(des => des.Halls.HallNo))
.ForMember(d => d.MovieDateTime  , s => s.MapFrom(des => des.MovieDateTime));
public YourController(ILogger<YourController> logger,             
             IMapper mapper)
        {
            Logger = logger;            
            Mapper = mapper;            
        }

        public ILogger<YourController> Logger { get; }

        public IMapper Mapper { get; }

See if this helps to solve your issue. Please provide the Entities details if you still have issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM