简体   繁体   English

当每种房型有多种时,如何使用LINQ找到可用的房间?

[英]How do I find available rooms using LINQ, when there are more than one of each room type?

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: 正是在这一点 - 检查可用性 - 我使用了以下LINQ表达式:

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) roomChoiceID是通过用用户输入的房间选择查询数据库获得的)

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). (例如,预订10月/ 18日的家庭活动室 - 12/06/18工作正常,但在这些日期尝试预订第二个家庭活动室会出错)。 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. 我完全不确定如何使用LINQ给我一个房间类型相同的房间(在我的例子中是家庭),而不是在我输入的日期预订。

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: 或者,使用linq表达式:

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. 说明:在上面的查询中,它将返回所有未在到达和离开范围内预订的房间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM