![](/img/trans.png)
[英]ASP.NET MVC: How can I put the StartDate of a current record on the EndDate of an earlier record
[英]Asp.net MVC Booked StartDate and EndDate check
我想检查我的第一次预订 res2.StartDate 和 res2.EndDate 是否与特定房间的 res1.StartDate 和 res1.EndDate 不同。 而且不在 res1.StartDate 和 res1.EndDate 之间,所以我可以预订房间。
我已经这样做了
var result = from r in _reservationData.GetAll()
.Where(r => r.RoomId == reservation.RoomId)
.Where(r => r.StartDate == reservation.StartDate)
.Where(r => r.EndDate == reservation.EndDate)
orderby r.StartDate
select r;
但这只是检查它们是否相等。 如何检查预订 2 期间是否不在预订 1 期间之间?
先感谢您。
这是未更新:
private bool CheckIfThisRoomIsAvailable(Reservation reservation)
{
// return the room data
var result3 = _roomData.GetById(reservation.RoomId);
// return the reservations of this room and
// check if the startdate of the new reservation
// is greater than the enddate of the old reservation
var result1 = from r in _reservationData.GetAll()
.Where(r => (r.RoomId == reservation.RoomId)
&& !(r.EndDate < reservation.StartDate))
select r;
/* return the reservations of this room and check if the startdate
of the new reservation less than the enddate of old reservation
and check if the enddate of the new reservation is less than
the startdate of the old reservation
*/
var result2 = from r in _reservationData.GetAll()
.Where(r => (r.RoomId == reservation.RoomId)
&& !(r.EndDate > reservation.StartDate)
&& !(r.StartDate > reservation.EndDate))
select r;
// checking condtions
if (!result3.Status.Contains("Available"))
{
return false;
}
else if (result1 is null)
{
return true;
}
else if (result2 is null)
{
return true;
}
else
{ return false; }
}
但问题是如果条件,result1 或 2 为空,它返回 false??
我用这种情况解决了这个问题:
var canIBookTheRoom =
!_reservationData.GetAll()
.Where(r => r.RoomId == reservation.RoomId)
.Where(r => (r.StartDate <= reservation.EndDate)
&& (r.EndDate >= reservation.StartDate)).ToList().Any();
// checking condtions
if (!result3.Status.Contains("Available"))
{
return false;
}
else if (canIBookTheRoom)
{ return true; }
else return false;
我希望这可以帮助任何人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.