I have the capability of a customer placing a booking after inputting their selected dates and specifying their room choice. It is at this point - checking availability - that I make use of the following LINQ expression:
var availRooms = db.Rooms.Where(room => room.RoomTypeID == roomChoiceID)
.Where(m => m.Bookings.All(r => r.Departure <= model.Arrival ||
r.Arrival >= model.Departure));
( roomChoiceID
is obtained by querying the db with the user's inputted room choice)
This works fine, up until I decide to book two rooms of the same room type on the same dates. (Eg booking a family room for 10/06/18 - 12/06/18 works fine, but then attempting to book the second family room on these dates gives an error). I am totally unsure how I can use LINQ to give me the room which has the same room type (family in my example), and is NOT booked on the dates I input.
var availRooms = db.Rooms.Where(room => room.RoomTypeID == roomChoiceID && (!room.Booking.Any(b => (b.Departure >= model.Departure && b.Arrival <= model.Arrival)));
Or, using linq expression:
var availRooms = from room in rooms
where room.RoomTypeID == roomChoiceID
&& (!room.Bookings.Any(b => b.Departure >= model.Departure && b.Arrival <= model.Arrival))
select room;
Explanation: In the query above, it will return all rooms where has not booking within the Arrival and Departure range.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.