【发布时间】:2021-11-29 15:23:24
【问题描述】:
我正在尝试检索具有screenings 列表的movie 对象。 screenings 中的每个对象都有一个 theater 对象,而该对象又具有一个 screenings 列表。我试图摆脱theater 中的screenings 列表,所以我创建了一个theaterDTO 模型。
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
movies和theaters之间存在多对多关系,screenings是桥接表。我正在尝试选择带有screenings列表的电影对象。screenings中的每个对象都有一个theater。我希望theater只有两列并省略其余列。 -
你能发布你的筛选课程吗?
-
@Serge 我已经添加了课程
-
谢谢,抱歉,您能否也添加剧院和电影
标签: c# asp.net entity-framework asp.net-core