【问题标题】:Select() does not work inside ThenInclude()Select() 在 ThenInclude() 中不起作用
【发布时间】:2021-11-29 15:23:24
【问题描述】:

我正在尝试检索具有screenings 列表的movie 对象。 screenings 中的每个对象都有一个 theater 对象,而该对象又具有一个 screenings 列表。我试图摆脱theater 中的screenings 列表,所以我创建了一个theaterDTO 模型。

what I'm trying to achieve

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

这会导致screening 列表被多次检索:在movie 对象和movie.theaters 中的每个theater 对象中

这是Screening 类:

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

功能:

[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();
    }

【问题讨论】:

  • Theater 不是一个列表,它是一个对象。你想选择什么?
  • @Blindy moviestheaters 之间存在多对多关系,screenings 是桥接表。我正在尝试选择带有screenings 列表的电影对象。 screenings 中的每个对象都有一个 theater。我希望 theater 只有两列并省略其余列。
  • 你能发布你的筛选课程吗?
  • @Serge 我已经添加了课程
  • 谢谢,抱歉,您能否也添加剧院和电影

标签: c# asp.net entity-framework asp.net-core


【解决方案1】:

首先定义一个 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; }
}

这样的拍摄数据:

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();
               

【讨论】:

  • 感谢您的回复。抛出此错误:CS0266: Cannot implicitly convert type "System.Linq.IQueryable&lt;&lt;anonymous type: System.Collections.Generic.IEnumerable&lt;Data.Entities.TheaterDTO&gt; Screening&gt;&gt;" to "Data.Entities.Movie"
  • 好吧,也许编辑后的版本可以工作@user321
  • 略有不同的错误:CS0266: Cannot implicitly convert type "System.Linq.IQueryable&lt;&lt;anonymous type: System.Collections.Generic.IEnumerable&lt;&lt;anonymous type: string Name, string Address&gt;&gt; Screening&gt;&gt;" to "Data.Entities.Movie"
  • 哎呀,我忘了加ToList。再次检查@user321
  • 这次是:CS0266: Cannot implicitly convert type "System.Collections.Generic.List&lt;&lt;anonymous type: System.Collections.Generic.List&lt;&lt;anonymous type: string Name, string Address&gt;&gt; Screening&gt;&gt;" to "Data.Entities.Movie"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多