LINQ:分组和加入

  • 本文关键字:LINQ c# linq
  • 更新时间 :
  • 英文 :


我有两个模型:

public partial class Movie
{
    public Movie()
    {
        TimeTables = new HashSet<TimeTable>();
    }
    [Key]
    public int MovieId { get; set; }
    public string MovieName { get; set; }
    public int MovieGenre { get; set; }
    public string MoviePicture { get; set; }
    public string MovieDescription { get; set; }
    public string MovieShortText { get; set; }
    public bool? MovieIs3d { get; set; }
    public bool? MovieIsImax { get; set; }
    public int MovieLanguage { get; set; }
    public bool? MovieSubtitled { get; set; }
    public int? MovieMinimalAge { get; set; }
    public bool? MovieHasDrugs { get; set; }
    public bool? MovieHasViolence { get; set; }
    public bool? MovieHasSex { get; set; }
    public bool? MovieHasSwearing { get; set; }
    public bool? MovieIsScary { get; set; }
    public bool? MovieHasDiscrimination { get; set; }
    public string MovieTrailer { get; set; }
    public int MovieLength { get; set; }
    public int? Genre_GenreId { get; set; }
    public int? Language_LanguageId { get; set; }
    public virtual Genre Genre { get; set; }
    public virtual Language Language { get; set; }
    public virtual ICollection<TimeTable> TimeTables { get; set; }
}

:

public partial class TimeTable
{
    public TimeTable()
    {
        Reservations = new HashSet<Reservation>();
    }
    public int TimeTableId { get; set; }
    public int MovieId { get; set; }
    public int RoomId { get; set; }
    public int SeatsAvaible { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
    public virtual Movie Movie { get; set; }
    public virtual ICollection<Reservation> Reservations { get; set; }
    public virtual Room Room { get; set; }
}

我想显示所有的记录从电影有一个或多个记录在时间表和StartDate。Date ==[给定的日期时间]。通过一个简单的查询,电影正在多次放映。我试过不同的方法,但无济于事。有人有解决办法吗?

当前查询:

var times2 =
            (from s in timetablerepo.TimeTables
             orderby s.StartTime.TimeOfDay
             where s.StartTime.Date == datetime.Date
             select s).Distinct().ToList();

为什么不先从电影开始,然后按时间表筛选:

var times = timetablerepo.Movies
            .Where(m => m.TimeTables.Any(t => t.StartDate.Date == <yourdate>));

相关内容

  • 没有找到相关文章

最新更新