我有以下功能:
public IList<Movie> GetMovieTimeTable(DateTime date)
{
var result = (from m in movierepo.Movies
join t in timetablerepo.TimeTables on m.MovieId equals t.MovieId
where t.StartTime.Date == date.Date
select m).Distinct().ToList();
return result;
}
可以正常工作,但是where条件不起作用。仍然返回timeableepo中具有相同MovieId的所有记录。原因是什么?我做错了什么?正确的做法是什么?
编辑:例子:
我有一个记录在电影与MovieId = 34在时间表有3个记录:
MovieId StartTime
34 08-06-2015 19:00
34 09-06-2015 21:00
34 10-06-2015 20:00
(Dutch DateTime Format)
给定'date'中的日期,我想将记录过滤到当天。(此时时间很重要)。现在我得到了所有这些记录。
示例2:
DateTime日期= {8-6-2015 00:00:00}
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..)
编辑3:电影模式:
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; }
}
编辑4:对电影时间表的查询也是可能的,但然后我得到多个电影记录,因为每个电影的时间表中有超过1条记录,我在查询中不太好,但如果你有解决方案,我会很高兴听到:)
最终,我认为问题在于您试图重用数据类来保存视图数据。最简单的方法是创建一组新的类,但这可能会让您:
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;
}