2010-05-13 27 views
8

如何將以下SQL查詢轉換爲可比較的LINQ查詢?如何在LINQ中編寫「不在」SQL查詢?

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

此外,LinqPad以書面形式幫助一個方便的工具和測試LINQ查詢在這種情況下 – CaffGeek 2010-05-13 20:08:21

+0

你也可以使用邏輯否定:其中工資<= 100 – 2010-05-13 20:25:18

+0

@ Chad..ya將嘗試似乎有用 @ CrazyJugglerDrummer以及我正在嘗試做一些更復雜的事情...亞,但在這種情況下,它會:)... – Vishal 2010-05-13 20:51:59

回答

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

這不是在邏輯上相同。設想一個沒有僱員的部門(原始查詢返回它,這個查詢不)。 – 2010-05-13 20:41:00

+0

我很高興你有你在找什麼。 – Rob 2010-05-13 21:12:04