我如何将此SQL代码转换为LINQ?

时间:2012-06-14 23:52:59

标签: 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

2 个答案:

答案 0 :(得分:4)

这应该可以解决问题:

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

答案 1 :(得分: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;