Home > Enterprise >  Select() does not work inside ThenInclude()
Select() does not work inside ThenInclude()

Time:10-11

I am trying to retrieve a movie object that has a screenings list. Every object in screenings has a theater object that in turn has a screenings list. I am trying to get rid of that screenings list in theater so I created a theaterDTO model.

what I'm trying to achieve

    var x = _context.Movies
                .Where(m => movieId == m.Id)
                .Include(m => m.Screenings)

this results in screening list being retrieved multiple times: in movie object and in every theater object in movie.theaters

Here's the Screening class:

public class Screening : BaseEntity
    {
        public Theater Theater { get; set; }
        public Movie Movie { get; set; }
        public DateTime Time { get; set; }

        public ICollection<Booking> Bookings { get; set; }    
        public Screening()
        {
            Bookings = new List<Booking>();
        }
    }

Movie:

public class Movie : BaseEntity
    {
        public string Name { get; set; }
        public string Body { get; set; }
        public string Dir { get; set; }
        public string Pic { get; set; }

        public ICollection<Screening> Screenings { get; set; }
        public Movie()
        {
            Screenings = new List<Screening>();
        }
    }

Theater:

public class Theater : BaseEntity
    {
        public string Address { get; set; }
        public string Name { get; set; }

        public ICollection<Screening> Screenings { get; set; }
        public Theater()
        {
            Screenings = new List<Screening>();
        }
    }

TheaterDTO:

public class TheaterDTO 
    {
        public string Address { get; set; }
        public string Name { get; set; }
    }

the function:

[HttpGet]
    [Route("{movieId}")]
    public Movie GetMovieById(Guid movieId)
    {
        return _context.Screenings
            .Where(m => movieId == m.Id)
            .Select(x => new TheaterDTO
            {
                Name = x.Theater.Name,
                Address = x.Theater.Address
            }).FirstOrDefault();
    }

CodePudding user response:

if you want movie and theaters together, you have to create Dto that includes movie and theaters, since Movie class doesn't have theaters

var x = _context.Movies
  .Where(m =>  m.Id==movieId)
 .Select(x => new MovieDto
{
    Movie = x,
    Theaters = x.Screenings
    .Select(x =>  new  TeatherDto{
              Name = x.Theater.Name,
              Address = x.Theater.Address
      })
     .ToList()
 }).FirstOrDefault();

dto

public MovieDto
{
  public Movie Movie {get; set;}
  public List<TheaterDto> Theaters {get; set;}
}

CodePudding user response:

Firt define a MovieDTO:

public class MovieDTO
{
    public string Name { get; set; }
    public string Body { get; set; }
    public string Dir { get; set; }
    public string Pic { get; set; }
    public List<ScreeningDTO> Screenings { get; set; }
}

public class ScreeningDTO
{
    public TheaterDTO Theater { get; set; } 
}

public class TheaterDTO
{
    public string Address { get; set; }
    public string Name { get; set; }
}

the take data like this:

var x = _context.Movies
            .Where(m => movieId == m.Id)
            .Include(m => m.Screenings)
               .ThenInclude(s => s.Theater)
            .Where(p => p.Id == movieId)
            .Select(p => new MovieDTO {
                      Name = p.Name,
                      Body = p.Body,
                      Dir = p.Dir,
                      Pic = p.Pic,
                      Screening = p.Screenings.Select(x => new ScreeningDTO {
                                     Theater = new TheaterDTO {
                                        Name = x.Theater.Name,
                                        Address = x.Theater.Address
                                    }
                       }).ToList()
                    }).FirstOrDefault();
               
  • Related