LINQ query to return list where condition is based on another table

Viewed 67

I have two tables Cars and Booking.

I want the following SQL query's equivalent in LINQ and I'm struggling with it

The SQL query is:

SELECT * 
FROM CARS 
WHERE Car_id NOT IN (SELECT Car_Id 
                     FROM Bookings 
                     WHERE JourneyCompletion = 'OnGoing');

The result should be a list of cars. How can I achieve this in Linq?

1 Answers

Try Below Code:

var Q1 = (from row in DbContext.Bookings where row.JourneyCompletion=='OnGoing' select row).ToList();
var Q2 = (from row in DbContext.CARS.where(n=> !n.Car_Id.Contains(n.Car_id))).ToList();
Related