LINQ:分组并加入

时间:2015-06-10 23:40:58

标签: c# linq

我有2个型号:

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

我想显示来自Movie的所有记录,这些记录在TimeTable中有一个或多个记录,其中StartDate.date == [给定日期时间]。 通过简单的查询,电影可以多次显示。我尝试了一个distinct(),但没有改变。 这里有谁有解决方案?

当前查询:

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

1 个答案:

答案 0 :(得分:1)

为什么不先从电影开始,按时间表过滤:

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