gpt4 book ai didi

c# - SQL Linq 预留可用性

转载 作者:行者123 更新时间:2023-11-29 19:03:52 26 4
gpt4 key购买 nike

我想创建一个预订系统,该系统具有在预订前检查房间可用性的功能。我正在 MVC asp net 中执行此操作。

但是,我在调用 SQL 来检查可用性时遇到问题,因为“我的预订”和 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 表达式格式错误。任何和每一个帮助将不胜感激,因为我快要抓狂了!

最佳答案

更好地了解您到底想从结果中得到什么会更好。我只能根据你当前的 SQL 进行猜测。当我看到没有 JOIN 且有许多 SELECT 语句的 SQL 时,我会犹豫(这并不意味着它完全错误,只是有时是一个危险信号。)

根据您提供的信息,我只能告诉您当前的 SQL 出了什么问题。

改变

where ReservationId = 

where ReservationId IN 

In 将评估所有结果。

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

关于c# - SQL Linq 预留可用性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43663255/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com