SQL Linq预订可用性

时间:2017-04-27 16:27:02

标签: c# mysql asp.net asp.net-mvc linq

我想创建一个预订系统,其功能可在预订前检查房间的可用性。我在MVC asp.net中这样做。

我遇到了调用SQL的问题,因为My Reservation和RoomType表有多对多的关系,因此检查可用性。

我的表和数据模型如下:

Reservation_RoomType enter image description here

DataModel

enter image description here

到目前为止,我已经设法自己提出以下内容:

Select * from RoomTypes
 where id = 3 and not exists
    (select * from Reservation_RoomType 
    where ReservationId = 
        (select id from Reservations where Arrival <= '2017-05-18' and Depature >= '2017-05-20' ))

在Linq

        var query = (from rooms in context.RoomTypes
                     where (rooms.Id == modelid) &&
                     !(context.Reservation_RoomTypes.Any(a => a.ReservationId ==
                               (from reservations in context.Reservations where (reservations.Arrival <= DateTime.Parse("2017-05-18"))
                                && (reservations.Depature => DateTime.Parse("2017-05-18"))) select *);

由于我目前的播种,这应该不返回任何房间,但如果我将房间ID更改为应该可用的其他内容,则仍然显示没有房间。 SQL不是我的强项,我很肯定我的linq表达式格式错误。任何和每一个帮助都会受到赞赏,因为我撕裂了我的头发!

1 个答案:

答案 0 :(得分:0)

更好地了解您希望从结果中得到什么。我只能根据你当前的SQL来猜测。当我看到没有JOIN并且有很多SELECT语句的SQL时,我会犹豫不决(这并不意味着它总是错误的,有时只是一个红旗。)

根据您提供的信息,我只能告诉您当前SQL的错误。

更改

where ReservationId = 

where ReservationId IN 

In将评估所有结果。

如果这不起作用,请从结果中更好地描述您想要的内容,您的确切评价是什么。

相关问题