-1

I have been playing around with left joins in linq and found that it is necessary to use DefaultIfEmpty() in order to change the join from an inner to a left.

Looking at the return type of DefaultIfEmpty(), I became concerned from a performance perspective as I see that it's returning an IEnumerable, which we know caches the data locally. In my scenario, I am further processing the data post left joining and would ideally like to preserve the type as IQueryable instead, however when I deployed the solution as is to my dev environment, I was surprised to see a counter-intuitive/minimal performance impact.

Why is it that the DefaultIfEmpty() does not seem to have a significant performance impact, or am I not understanding the situation properly?

Edit: Below is my code: enter image description here

Luke
  • 776
  • 9
  • 24

1 Answers1

2

Ther are both IQueryable and IEnumerable overloads of DefaultIfEmpty(). IEnumerable version is used on inmemory Linq and IQueryable version with expressions for example with EF.

If you began with a Queryable and end up with a IEnumerable you consume your Queryable somwhere along the line

enter image description here

Anders
  • 17,306
  • 10
  • 76
  • 144
  • Thanks! This example looked awesome so I tried to copy it but surprisingly this breaks something with postgres and throw the exceptaion: `InvalidOperationException: An exception has been raised that is likely due to a transient failure., at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute(TState state, Func operation, Func> verifySucceeded) in NpgsqlExecutionStrategy.cs, line 37` – Luke Mar 28 '19 at 13:56
  • The select many? Yeah its a nifty way of doing left joins. It works fine with SQL and EF – Anders Mar 28 '19 at 14:24