如何在LINQ中编写“Not In”SQL查询?

时间:2010-05-13 20:01:48

标签: c# sql linq

如何将以下SQL查询转换为可比较的LINQ查询?

select * from Dept 
where Id not in (
    Select Id 
    from Employee 
    where Salary > 100);

2 个答案:

答案 0 :(得分:15)

尝试这样的事情:

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;

答案 1 :(得分:0)

这个怎么样?

var lowPaidEmps = from d in db.Dept 
                  join e in db.Employees on d.Id equals e.Id 
                  where e.Salary <= 100
                  select d;