在哪里和加入LINQ是行不通的

时间:2015-06-08 17:10:49

标签: c# linq join where

我有以下功能:

MovieId StartTime 
34 08-06-2015 19:00 
34 09-06-2015 21:00 
34 10-06-2015 20:00 
(Dutch DateTime Format) 

它工作正常,但where条件不起作用。仍然返回来自具有相同MovieId的timetablerepo的所有记录。 这是什么原因?我做错了什么?这样做的正确方法是什么?

编辑:示例:

我在MovieId = 34的电影中有记录在TimeTable中有3条记录:

Result.Count=6 (That's right)
[0] 
MovieId = 2
(Etc..)
   TimeTables
   [0]
   MovieId = 2
   StartTime = {5-5-2015 20:00:00}
   (Etc..)
   [1]
   MovieId =2 
   StartTime = {28-5-2015 20:00:00}
   (Etc..)
[1]
MovieId = 13
TimeTables
   [0]
   MovieId = 13
   StartTime = {28-5-2015 21:00:00}
   (etc..)
   [1]
   MovieId = 13
   StartTime = {28-5-2015 19:50:00}
   (etc..)
(etc..)

鉴于'date'中的日期,我想过滤当天的记录。 (此时此刻重要)。现在我得到了这些记录。

示例2:

日期时间     日期= {8-6-2015 00:00:00}

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

编辑3: 电影模特:

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

时间表:

>>> socket.gethostbyaddr('8.8.8.8')
('google-public-dns-a.google.com', ['8.8.8.8.in-addr.arpa'], ['8.8.8.8'])

编辑4: 关于TimeTable to Movie的查询也是可能的,但后来我得到了多个电影记录,因为每个电影的时间表中有超过1条记录,而且我的查询不太好,但是如果你有解决方案,我会听到:)

1 个答案:

答案 0 :(得分:0)

最终,我认为问题在于您尝试重用数据类来保存View数据。最简单的方法就是创建一组新的类,但这可能会让你:

public IList<Movie> GetMovieTimeTable(DateTime date)
{
   var result = movierepo.Movies
     .Join(timetablerepo.TimeTables,m=>m.MovieId,tt=>tt.MovieId,
       (m,g)=>new Movie{
         MovieId=m.MovieId,
         ... rest of properties here ...
         TimeTables=g.Where(tt=>tt.StartDate.Date==date.Date).ToList()
       })
     .Where(m=>m.TimeTables.Any())
     .ToList();
   return result;
}