How to perform a Left Join with LINQ C# using my SQL?

Asked

Viewed 488 times

-1

Example that works in SQL:

select * from Doacoes
inner join Projetos on Projetos.IdProjeto = Doacoes.IdProjeto
left join Pedidos on Doacoes.IdDoacao = Pedidos.IdDoacao
left join Recompensas on Pedidos.IdRecompensa = Recompensas.IdRecompensa
where Doacoes.IdUsuario = 2017

I performed the rewrite this way, but it doesn’t work:

from d in banco.Doacoes
join p in banco.Projetos on d.IdProjeto equals p.IdProjeto
join pedi in banco.Pedidos on d.IdDoacao equals pedi.IdDoacao into pedi
join recompensa in banco.Recompensas  on recompensa.IdRecompensa equals 
pedi.IdRecompensa into pedi
  • If I’m not mistaken it’s the same thing, left join pedi in.... 3 years I don’t use Linq but I think that’s it msm!

1 answer

0

To have the left Join in the Entity Framework you will have to do the following

from d in banco.Doacoes
join p in banco.Projetos on d.IdProjeto equals p.IdProjeto
join pedi in banco.Pedidos on d.IdDoacao equals pedi.IdDoacao into pediLeft
from pedi in pediLeft.DefaultIfEmpety()
join recompensa in banco.Recompensas on recompensa.IdRecompensa equals pedi.IdRecompensa into recoLeft
from recompensa in recoLeft.DefaultIfEmpety()

Take a look at that question Entity Framework Left Join also.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.