tags:

views:

221

answers:

2

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);
+13  A: 

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;
Andrew Hare
Thanks..it works
Misnomer
A: 

How about this?

var lowPaidEmps = from d in db.Dept 
                  join e in db.Employees on d.Id equals e.Id 
                  where e.Salary <= 100
                  select d;
Rob
That's not logically equivalent. Imagine a department with no employees (original query returns it, this query does not).
David B
I'm glad you got what you're looking for.
Rob