Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ, Left Join, Only Get where null in join table

Tags:

c#

left-join

linq

I am trying to do a left outer join on two tables, but I only want to return the results from the first table where the second table does not have a record (null).

 var agencies = from a in agencyList
                           join aa in joinTable
                           on a.AgencyId equals aa.AgencyId into joined
                           from aa in joined.DefaultIfEmpty()
                           where aa == null)
                           select a;

But this does not exclude the non null values of aa, and returns all the records just the same as if the 'where aa == null' was not there.

Any help is appreciated. Thanks.

like image 713
kmehta Avatar asked Jan 27 '26 15:01

kmehta


1 Answers

What about:

var agencies = from a in agencyList
                           where (from aa in joinTable where aa.AgencyId == a.AgencyId select aa).Count() == 0
                           select a;
like image 56
Brian Avatar answered Jan 29 '26 03:01

Brian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!