谁能帮我把这个 SQL 转换成 LINQ?
SELECT * FROM Room_Type b
WHERE Exists(
SELECT *
FROM Room a
WHERE a.RoomId NOT IN
(SELECT r.RoomId
FROM Reservation r
WHERE NOT (r.DepartureDate<='2015-02-16' OR r.ArrivalDate > '2015-02-20' )
)
)
谁能帮我把这个 SQL 转换成 LINQ?
SELECT * FROM Room_Type b
WHERE Exists(
SELECT *
FROM Room a
WHERE a.RoomId NOT IN
(SELECT r.RoomId
FROM Reservation r
WHERE NOT (r.DepartureDate<='2015-02-16' OR r.ArrivalDate > '2015-02-20' )
)
)
您可以使用连接简化查询:
SELECT *
FROM Room_Type rt
JOIN room r
ON r.RoomTypeId = rt.Id
JOIN Reseration res
ON res.RoomId = r.Id
WHERE NOT (r.DepartureDate<='2015-02-16' OR r.ArrivalDate > '2015-02-20' )))
然后您可以按如下方式使用 linq:
DateTime departure = new DateTime(2015,02,16);
DateTime arrival = new DateTime(2015,02,20);
context.Reservation.Where(r.DepartureDate<= departure || r.ArrivalDate > arrival ).Select(res => res.Room.Room_Type)