Considering Extension Methods about Entity Framework, there is not exactly the concept of LEFT JOIN
. What there is is a charge of Responsavel
who may or may not have Filho
.
I mean, I imagine your Responsavel
is modelled as follows:
public class Responsavel
{
...
public virtual ICollection<Filho> Filhos { get; set; }
}
public class Filho
{
...
public virtual Responsavel Responsavel { get; set; }
}
In this case, the selection would be as follows:
var responsaveisEFilhos = db.Responsavel
.Include(r => r.Filhos)
.ToList();
Only this selects all Responsaveis
and their Filhos
. If you only want the three fields, you stay like this:
var responsaveisEFilhos = db.Responsavel
.Include(r => r.Filhos)
.ToList();
var listaComoJoin = responsaveisEFilhos
.SelectMany(r => r.Filhos)
.Select(f => new
{
ResponsavelId = f.ResponsavelId,
ResponsavelNome = f.Responsavel.Nome,
FilhoNome = f.Nome
})
.ToList();
But note that this gets a little out of hand. You had already selected Filho
in the first command, but without denormalizing the data. Using the Extension Methods, the way of thinking in selection is no longer like a join
SQL, and yes as an entity and its dependent data.
Now, if you really wants to use Linq, so it looks like this:
var responsaveisEFilhos = (
from responsaveis in db.Responsaveis
from filhos in db.Filhos
.Where(f => f.ResponsavelId == responsaveis.Id)
select new {
ResponsavelId = responsaveis.Id,
ResponsavelNome = responsaveis.Nome,
FilhoNome = filhos.Nome
}
).ToList();
But this has no optimization at all, and you don’t work with all the information of the entities, something I see as a disadvantage.
Does it have to be Linq exactly? Or can it be Extension methods of EF?
– Leonel Sanches da Silva
http://stackoverflow.com/questions/19356439/left-join-in-linq-to-entities I think you can help here.
– Marconi
Yes @Ciganomorrisonmendez , but what would that be Extension methods EF? I am using the same Entity Framework!
– Jedaias Rodrigues
@Marconi does not know very well English friend... And Translate splinters everything.
– Jedaias Rodrigues
Linq looks like an SQL. Extension Methods is that RU mode more or less like this:
db.Entidade.Where(...).Select(...)
. Got it?– Leonel Sanches da Silva
Yeah, great! Could be in Extension Methods same. Please consider
db.Responsavel
anddb.Filho
. I would be very grateful if you could help me @Ciganomorrisonmendez– Jedaias Rodrigues
@Jedaiasrodrigues ok, I think the Gypsy will help you better.
– Marconi