我目前正在开发车辆预订系统,用户可以从下拉列表中选择汽车,但是某些汽车在特定日期(例如星期一)不能使用。有没有一种方法可以根据日期排除汽车?
编辑
在选择车辆之前添加了用于预订功能的代码
public ActionResult ReserveInitial()
{
return View();
}
[HttpPost]
public ActionResult ReserveInitial(ReservationViewModel RVM)
{
var repo = new ManagementRepository();
var resini = new ReservedVehicleDetail
{
Id = RVM.Id,
BorrowerLastName = RVM.BorrowerLastName,
BorrowerFirstName = RVM.BorrowerFirstName,
VehicleID = RVM.VehicleID,
Reason = RVM.Reason,
ProjectID = RVM.ProjectID,
Location = RVM.Location,
DateTimeStart = RVM.DateTimeStart,
DateCreated = DateTime.Now,
UserID = RVM.UserID,
DriverID = RVM.DriverID,
IsActive = false
};
repo.ReserveInitial(resini);
//TempData["ID"] = resini.Id;
//TempData.Keep();
return RedirectToAction("ChooseVehicle", "Reservation", new { id = resini.Id });
}
这是我的选车代码:
public ActionResult ChooseVehicle(int? id)
{
// int id = (int)TempData["ID"];
ReservedVehicleDetail resDet = db.ReservedVehicleDetails.Find(id);
var RVM = new ReservationViewModel();
ViewBag.VehicleID = db.Vehicles.Select(a => new SelectListItem
{
Value = a.VehicleID.ToString(),
Text = a.VehicleMake + " - " + a.PlateNumber
});
// ViewBag.VehicleID = new SelectList(db.Vehicles, "VehicleID", "VehicleMake");
RVM.Id = resDet.Id;
RVM.BorrowerLastName = resDet.BorrowerLastName;
RVM.BorrowerFirstName = resDet.BorrowerFirstName;
RVM.Reason = resDet.Reason;
RVM.ProjectID = resDet.ProjectID;
RVM.Location = resDet.Location;
RVM.DateTimeStart = resDet.DateTimeStart;
RVM.DateCreated = resDet.DateCreated;
RVM.UserID = resDet.UserID;
RVM.IsActive = resDet.IsActive;
//RVM.DriverID = resIni.DriverID;
TempData.Keep();
return View(RVM);
}
[HttpPost]
public ActionResult ChooseVehicle(ReservationViewModel RVM)
{
var repo = new ManagementRepository();
var resDet = new ReservedVehicleDetail();
// int id = (int)TempData["ID"];
using (var db = new VehicleReservationEntities())
{
var existingReservation = (from data in db.ReservedVehicleDetails
where data.Id == RVM.Id
select data).FirstOrDefault();
existingReservation.Id = RVM.Id;
existingReservation.BorrowerLastName = RVM.BorrowerLastName;
existingReservation.BorrowerFirstName = RVM.BorrowerFirstName;
existingReservation.VehicleID = RVM.VehicleID;
existingReservation.Reason = RVM.Reason;
existingReservation.ProjectID = RVM.ProjectID;
existingReservation.Location = RVM.Location;
existingReservation.DateTimeStart = RVM.DateTimeStart;
existingReservation.DateCreated = RVM.DateCreated;
existingReservation.UserID = RVM.UserID;
existingReservation.IsActive = false;
//existingVehicle.DriverID = RVM.DriverID;
repo.chooseVehicle(existingReservation);
}
return RedirectToAction("DriverCheck", "Reservation", new { id = RVM.Id });
}
答案 0 :(得分:1)
回答您的问题: 是的,这在SQL中有点令人困惑,但是我将解释如何做。 您想过滤掉星期一,所以您得到的日期与星期日开始的日期不同
例如1900年1月7日,星期日, 由于星期日是一周的开始(索引0),因此,如果对日期进行取模,则可以从该日期创建一个过滤器。
DayOfWeek d = DayOfWeek.Monday;
var vehicles = db.ReservedVehicleDetails.Where(x =>
DbFunctions.DiffDays(new DateTime(1900, 1, 7), x.someDateField) % 7 != (int)d);
(只需将someDateField替换为包含保留日期的字段)。
要解决您的问题 一般来说,您可能不想这样做,我想您需要更改架构,以便通过检查请求时间与预订时间是否重叠来获得可用的车辆。
db.Vechicles.Where(x => !x.Reservation.Any(r => r.StartDate <= requestedEndDate
&& requestedStartDate <= x.EndDate);
答案 1 :(得分:0)
您的$_GET['files']
GET操作将显示ChooseVehicle
可供选择的可用车辆列表,但尚不清楚用户是否正在请求哪个日期。例如,我想知道例如哪些日期是星期一,并且只显示适当的车辆供首先选择。您的要求可能还会遇到一些困难。整个一周但不允许星期一的车辆请求会怎样?
这不是您的问题的一部分,但我也考虑将您的ORM查询移出控制器操作,并移入一两个服务层类。通过使您可以更轻松地重用自己的代码,可以节省以后的时间和麻烦。
一旦您具有必要的信息以过滤到所需的结果,就可以通过数据库查询有效地进行过滤,因此您不必从数据库映射实体实例中提取记录到C#中,而只是决定排除它们。 / p>
在T-SQL中,研究使用ViewBag.VehicleID
函数,该函数在一周中的一天为您提供1-7的数字,以便仅提取逻辑上可以选择的载具进行显示。