Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a "Not In" SQL query in LINQ?

Tags:

c#

sql

linq

How would I translate the following SQL query in to a comparable LINQ query?

select * from Dept 
where Id not in (
    Select Id 
    from Employee 
    where Salary > 100);
like image 668
Vishal Avatar asked May 13 '10 20:05

Vishal


1 Answers

Try something like this:

var result = from d in Dept
             let expensiveEmployeeIds = (from e in Employee.Employees
                                       where e.Salary > 100
                                       select e.Id)
             where !expensiveEmployeeIds.Contains(d.Id)
             select d;
like image 152
Andrew Hare Avatar answered Oct 09 '22 04:10

Andrew Hare