如何使用LINQ实现左排除JOIN?

时间:2011-07-12 10:38:39

标签: c# linq linq-to-sql

如何使用LINQ实现左排除JOIN?

SQL中:

SELECT <select_list> 
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
WHERE B.Key IS NULL

3 个答案:

答案 0 :(得分:22)

DefaultIfEmpty()需要LEFT JOIN,然后您可以检查加入的值是否为null

var result = from a in Table_A
             join b in Table_B on a.Key equals b.Key into j
             from b in j.DefaultIfEmpty()
             where b == null
             select new { ... };

答案 1 :(得分:12)

更容易就是这样写:

var result = from a in Table_A
             where !Table_B.Any(b => b.Key == a.key)
             select new { ... };

答案 2 :(得分:1)

更快的方式

var result = from a in Table_A
    where !Table_B.Select(b => b.Key).Contains(a.Key)
    select new { ... };