gpt4 book ai didi

c# - 哪里和加入 LINQ 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:39:32 24 4
gpt4 key购买 nike

我有以下功能:

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 条件不起作用。仍然返回 timetablerepo 中具有相同 MovieId 的所有记录。这是什么原因?我做错了什么?正确的做法是什么?

编辑:示例:

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

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

鉴于“日期”中的日期,我想将记录过滤到那天。 (此时此刻时间很重要)。现在我得到了所有这些记录。

示例 2:

日期时间 日期 = {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 条记录,而且我不太擅长查询,但如果你有解决方案,我很乐意听到:)

最佳答案

最终,我认为问题出在您试图重用数据类来保存您的 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;
}

关于c# - 哪里和加入 LINQ 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30715194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com