我正在研究一個mvc應用程序,人們可以在其中搜索火車路線。我做了我的數據庫代碼 - 首先與實體框架,但我似乎無法弄清楚如何問一些更復雜的查詢。Linq - 包含和過濾器 - 嵌套集合
我有我在viewmodel中使用的路線的集合。因此,第一步是詢問哪些路線具有特定的起點和終點站。然後,我想包括某個日期設置爲真的日程安排(以及該日程安排的開始日期和結束日期匹配)。這是一個旅行集合(我使用這張表導致路線一天運行多次)。從這裏我可以找到所有匹配的車站,並且從航線的HADSATION到達和離開時間。
,所以我想是這樣的:
public IEnumerable<Route> Search(DateTime date, int? departure, int? arrival)
{
var day = date.DayOfWeek.ToString();
return db.Routes.Where(r => r.DepartureStationID == departure && r.ArrivalStationID == arrival)
.Include(s => s.Train)
//using linq.dynamic here
.Include(r => r.Schedule.where(day + "==" + true)
.Include(sch => sch.trip.where(date > sch.DepartureTime)
.Include(route => route.RouteHaseStations)
.Include(st => st.Stations)
}
但這offcourse不工作。以下是我的型號:
public class Route
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int RouteID { get; set; }
public String RouteName { get; set; }
[ForeignKey("Train")]
public int TrainID { get; set; }
[ForeignKey("Station")]
public int DepartureStationID { get; set; }
[ForeignKey("Station")]
public int ArrivalStationID { get; set; }
public virtual ICollection<Schedule> Schedule { get; set; }
public virtual Train Train { get; set; }
public virtual Station DepartureStation { get; set; }
public virtual Station ArrivalStation { get; set; }
}
public class Station
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int StationID { get; set; }
[Display(Name = "Station")]
public String Name { get; set; }
public int Platforms { get; set; }
public float Latitude { get; set; }
public float Longitude { get; set; }
public virtual ICollection<RouteHasStation> RouteHasStation { get; set; }
}
public class Train
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int TrainID { get; set; }
public String Name { get; set; }
public virtual ICollection<Route> Route { get; set; }
}
public class Schedule
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ScheduleID { get; set; }
[ForeignKey("Route")]
public int RouteID { get; set; }
public Boolean Monday { get; set; }
public Boolean Tuesday { get; set; }
public Boolean Wednesday { get; set; }
public Boolean Thursday { get; set; }
public Boolean Friday { get; set; }
public Boolean Saturday { get; set; }
public Boolean Sunday { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public virtual ICollection<Trip> Trip { get; set; }
public virtual Route Route { get; set; }
}
public class Trip
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int TripID { get; set; }
[ForeignKey("Schedule")]
public int ScheduleID { get; set; }
public DateTime DepartureTime { get; set; }
public virtual ICollection<RouteHasStation> RouteHasStation { get; set; }
}
public class RouteHasStation
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int RouteHasStationID { get; set; }
[ForeignKey("Station")]
public int StationID { get; set; }
public virtual Station Station { get; set; }
[ForeignKey("Trip")]
public int TripID { get; set; }
public virtual Trip Trip { get; set; }
[DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm}")]
public DateTime? Arrival { get; set; }
[DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm}")]
public DateTime? Departure { get; set; }
public int Platform { get; set; }
public float Distance { get; set; }
}
請顯示錯誤。 – DumpsterDiver
有關過濾Include的問題在StackOverflow中很多。 –
歡迎來到Stack OverFlow。請在SO中查看一些示例問題,以便您瞭解如何正確提出問題以便從其他用戶獲得快速答案。 :)祝你好運 – Unbreakable