5

我正在尝试在我的酒店预订项目中实施按日期过滤。

有人可以帮我将此 sql 代码转换为 linq

SELECT r.*
FROM Room r LEFT JOIN Reservation v ON r.RoomID = v.RoomID
AND NOT (@StartDate >= Date_Check_Out OR @EndDate <= Date_Check_In)
AND v.cancel = 0
WHERE v.ReservationID IS NULL
4

3 回答 3

10

将 SQL 转换为 Linq 的好工具之一:Linqer

试试这个查询

var q = (from r in Room 
            join v in Reservation on r.RoomID equals v.RoomID  into outer
            from o in outer.DefaultIfEmpty()
            where !(o.Date_Check_Out<= startdate || o.Date_Check_In>=endDate)
                  && v.cancel == 0 && v.ReservationID == null 
            select r);

还要检查这个:

请参阅SQL to LINQ 工具 现有线程。

如果您决定手动操作,Linqpad应该很有用。

您还希望看到:SQL to LINQ (Visual Representation)一些很好的图形表示方法...

于 2012-05-04T07:29:44.937 回答
2

就像是:

DateTime startDate, EndDate;

var bookings = from r in Rooms
               join v in Reservation
                   on r.RoomID equals v.RoomID into RoomReservation 
                  from v in RoomReservation.DefaultIfEmpty()
               where
               (Date_Check_Out < startDate || Date_Check_In > endDate)
               select r;
于 2012-05-04T07:32:54.967 回答
1

也许是这样的:

DateTime StartDate=DateTime.Now;
DateTime EndDate=DateTime.Now; 

var result= (
    from r in Room
    from v in Reservation
        .Where(a=>
            a.RoomID == r.RoomID
            && 
            !(
                StartDate>=a.Date_Check_Out ||
                EndDate <=a.Date_Check_In
            )
            && a.cancel==false
        ).DefaultIfEmpty()
    where v.ReservationID == null
    select r
    );
于 2012-05-04T07:43:58.730 回答