tags:

views:

62

answers:

1

Is there any advantage in using either of these to retrieve elements from TableA that don't have a related element in TableB?

TableA
   .GroupJoin(
      TableB,
      o => o.TableAID,
      i => i.TableAID,
      (o,i) => new {o, child = i.DefaultIfEmpty()})
   .Where(x => x.child.Where(c => c != null).Count() == 0)
   .Select(x => x.o);

or

TableA
   .Where(a => !TableB.Select(b => b.TableAID).Contains(a.TableAID));

I'm used to doing this with a left outer join in SQL, which the first example kind of uses. The second example uses a "NOT IN" type of approach, which is not something I've used for this before.

Both ways return the same data. The second one would be my preferred one from simplicity view. Does the first one have any advantages?

Do you have another way of doing this?

A: 

SQL Server's query optimizer will do the same thing with a LEFT JOIN WHERE b is NULL as with a WHERE IN... Confirm this by looking at the estimated execution plan.

David B