2012-06-14 59 views
3

我該如何將此SQL代碼轉換爲LINQ?我該如何將此SQL代碼轉換爲LINQ?

SELECT * FROM tbl1 
    WHERE NOT EXISTS (SELECT NULL AS Expr1 
      FROM tbl2 AS tbl2_1 
      WHERE (T = 'literal') AND tbl1.Id = someId) 

這是我有什麼,但它不工作:

from a in Tbl1 
let b = from b in Tbl2 select b.someId 
let c = from c in Tbl2 select c.T 
where (!c.Contains("literal") & !(b.Contains(a.Id))) 
select a 
+4

請展開「不起作用」。 – Blorgbeard

回答

4

這應該做的伎倆:

from t1 in Tbl1 
where !(from t2 in Tbl2 
    where t2.T == "literal" 
    select t2.someId) 
     .Contains(t1.Id) 
select t1 
+0

是的,這是伎倆謝謝! – user1457485

+1

@用戶不客氣。由於我的迴應對您有用,請務必接受它作爲答案(單擊旁邊的複選標記)。 – arcain

0

你也可以試試這個:

var query = 
    from a in Tbl1 
    join b in Tbl2 on a.Id equals b.someId into bs 
    where !bs 
     .Where(b2 => b2.T == "literal") 
     .Any() 
    select a;