How to achieve Left Excluding JOIN using LINQ?
In SQL:
SELECT
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
WHERE B.Key I
You need DefaultIfEmpty()
for the LEFT JOIN
, then you can check if the joined value is 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 { ... };
An even faster way
var result = from a in Table_A
where !Table_B.Select(b => b.Key).Contains(a.Key)
select new { ... };
Easier would be to write like this:
var result = from a in Table_A
where !Table_B.Any(b => b.Key == a.key)
select new { ... };