【发布时间】:2019-06-02 18:39:13
【问题描述】:
我有这两个实体:
public class Ticket
{
public int Id { get; set; }
public int ScheduleId { get; set; }
public int SeatId { get; set; }
[DataType(DataType.Date)]
[Column(TypeName = "Date")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime ForDate { get; set; }
public Seat Seat { get; set; }
public Schedule Schedule { get; set; }
public ICollection<TicketStop> TicketStops { get; set; }
}
public class TicketStop
{
public int Id { get; set; }
public int TicketId { get; set; }
public int LineStopStationId { get; set; }
public Ticket Ticket { get; set; }
public LineStopStation LineStopStation { get; set; }
}
public class LineStopStation
{
public int Id { get; set; }
public int LineId { get; set; }
public int StopId { get; set; }
public int Order { get; set; }
public bool IsLastStop { get; set; }
public Line Line { get; set; }
public Stop Stop { get; set; }
}
业务案例是我正在实施一个巴士票预订系统(主要用于学习目的)并且我想找到重叠的票。
LineId + ScheduleId + ForDate 的组合唯一标识一张票是在特定日期和出发时间的特定巴士。
我遇到的问题是在给定起点和终点位置的情况下确定两张票是否在一个或多个站点重叠。
LineStopStation 实体保存有关StopId 和Order 的信息,在公共汽车旅行期间访问它。所以基本上,重叠的车票在某些时候会有相同的Order 号码(除非它是最后一站,这意味着乘客要离开公共汽车)。
所以我拥有的是LineId、ScheduleId、StartId 和EndId,其中starId 和endId 对应于LineStopStation.StopId,所以最终我能够像这样从他们那里得到订单。
int startStationOrder = _context.LineStopStations
.First(l => l.LineId == lineId && l.StopId == startId).Order;
int endStationOrder = _context.LineStopStations
.First(l => l.LineId == lineId && l.StopId == endId).Order;
所以我非常确信拥有所有这些信息,我应该能够在TicketStop 表中找到与相关数据重叠的票证。 TicketStop 以这种方式工作 - 如果有人买了 3 站的票,我将在那里有三个记录,相同的 TicketId 和三个不同的 LineStopStationId。
我觉得这个问题比需要的要大。所以基本上我有这个:
public List<Seat> GetAvailableSeats(int lineId, int scheduleId, int startId, int endId, DateTime forDate)
{
int startStationOrder = _context.LineStopStations
.First(l => l.LineId == lineId && l.StopId == startId).Order;
int endStationOrder = _context.LineStopStations
.First(l => l.LineId == lineId && l.StopId == endId).Order;
var reservedSeats = _context.TicketStops
.Where(t => t.Ticket.ScheduleId == scheduleId)
.Where(t => t.Ticket.ForDate == forDate)
//basically I don't know how to proceed here.
//In pseudo code it should be something like:
.Where(t => t.Min(Order) >= endStationOrder || t.Max(Order) <= startStationOrder
}
但是obs。这不是LINQ 的工作方式。那么如何找到该范围重叠的所有票证呢?
【问题讨论】:
标签: c# linq-to-entities