[英]Where and join in LINQ is not working
I have the following function: 我有以下功能:
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;
}
It works fine, but the where condition doesn't work. 它工作正常,但where条件不起作用。 Still all records from timetablerepo with the same MovieId are returned. 仍会返回来自timetablerepo的所有具有相同MovieId的记录。 What's the reason for that? 是什么原因呢? What I am doing wrong? 我做错了什么? And what is the correct way to do this? 正确的方法是什么?
EDIT: Example: 编辑:示例:
I have an record in Movies with MovieId = 34 In TimeTable there are 3 records: 我在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)
Given the date in 'date' I want to filter the records to that day. 给定“日期”中的日期,我想过滤到当天的记录。 (Time is important at this moment). (此时此刻很重要)。 Now I'am getting al these records. 现在,我正在获取这些记录。
Example 2: 范例2:
DateTime Date = {8-6-2015 00:00:00} 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..)
Edit 3: Movie model: 编辑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; }
}
TimeTable: 时间表:
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; }
}
Edit 4: A query on TimeTable to Movie is also possible but then I get multiple movie records because there are more than 1 records in Timetable for each movie, and I'm not so good in query's, but if you have the Solution I would luke to hear :) 编辑4:也可以查询时间表到电影的时间表,但是我会得到多个电影记录,因为时间表中每个电影都有1条以上的记录,而我的查询条件不是很好,但是如果有解决方案,我会乐于听到:)
Ultimately, I believe the problem is because you are trying to reuse the data classes to hold your View data. 最终,我相信问题是因为您试图重用数据类来保存View数据。 It would be easiest to just create a new set of classes, but this might get you by: 仅创建一组新的类将是最简单的,但这可能会帮助您:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.