How to achieve Left Excluding JOIN using LINQ?

后端 未结 3 782
南方客
南方客 2021-01-17 08:04

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         


        
相关标签:
3条回答
  • 2021-01-17 08:13

    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 { ... };
    
    0 讨论(0)
  • 2021-01-17 08:32

    An even faster way

    var result = from a in Table_A
        where !Table_B.Select(b => b.Key).Contains(a.Key)
        select new { ... };
    
    0 讨论(0)
  • 2021-01-17 08:37

    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 { ... };
    
    0 讨论(0)
提交回复
热议问题