Convert sql to linq

I am trying to filter by date in my hotel booking project.

Can someone help me please convert this sql code to 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
+5
source share
3 answers

One of the good tools to convert SQL to Linq: Linqer

Try this request

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);

Also check this out:

See SQL to LINQ Tool  existing thread.

If you decide to do it manually, Linqpad should be helpful.

You would also like to see: SQL to LINQ (visual representation) is some good example using graphical representation ...

+10

- :

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;
+2

, - :

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
    );
+1

All Articles