I am trying to implement a LEFT OUTER JOIN in Linq against an Entity Framework Core 2.0 DbContext. It's important that the query is translated to SQL, rather than evaluated locally. I've reviewed several StackOverflow solutions including this one which is good, but none are using EF Core.
The problem I get is that EF Core returns the following warning/error for the DefaultIfEmpty()
method:
The LINQ expression 'DefaultIfEmpty()' could not be translated and will be evaluated locally
Without the DefaultIfEmpty()
method an INNER JOIN is used. My LINQ query looks like this:
var join = context.Portfolios
.Where(p => p.IsActive)
.GroupJoin(context.BankAccounts,
prt => prt.Id,
bnk => bnk.PortfolioId,
(prt, bnks) => new {Portfolio=prt,Account=bnks.DefaultIfEmpty()})
.SelectMany(r => r.Accounts.DefaultIfEmpty(),
(p, b) => new
{
Id = p.Portfolio.Id,
BankAccount = b.BankAccountNumber,
BankRef = b.BeneficiaryReference,
Code = p.Portfolio.Code,
Description = p.Portfolio.DisplayName
});
Does anyone know a way around this?