Reputation: 73
I wanted to check if my first reservation res2.StartDate and res2.EndDate are not the same of my res1.StartDate and res1.EndDate of a specific room. And also not in the between period of res1.StartDate and res1.EndDate so I can book the room.
I have done this
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;
But this is checking if they are equal only. How can I check if the reservation2 period not in the between of the reservation1 period??
Thank you in advance.
This is un update:
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; }
}
But the problem is with if condition, the result1 or 2 is null, it returns false??
Upvotes: 0
Views: 499
Reputation: 73
I solved the problem with this condition:
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;
I hope this help anyone!
Upvotes: 0