如何解决Asp.net MVC预订的StartDate和EndDate检查
我想检查我的第一次预订 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; }
}
但是问题在于如果条件,结果1或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;
我希望这对任何人都有帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。