Entity Framework Juntar 3 Tabelas
Pergunta
Eu estou tentando juntar três tabelas, mas eu não consigo entender o método...
Eu terminei de unir 2 tabelas
var entryPoint = dbContext.tbl_EntryPoint
.Join(dbContext.tbl_Entry,
c => c.EID,
cm => cm.EID,
(c, cm) => new
{
UID = cm.OwnerUID,
TID = cm.TID,
EID = c.EID,
}).
Where(a => a.UID == user.UID).Take(10);
Eu gostaria de incluir tbl_Title tabela com TID PK e obter Título de campo.
Muito obrigado
Solução
Eu acho que vai ser mais fácil usando a sintaxe de consulta com base em:
var entryPoint = (from ep in dbContext.tbl_EntryPoint
join e in dbContext.tbl_Entry on ep.EID equals e.EID
join t in dbContext.tbl_Title on e.TID equals t.TID
where e.OwnerID == user.UID
select new {
UID = e.OwnerID,
TID = e.TID,
Title = t.Title,
EID = e.EID
}).Take(10);
E provavelmente, você deve adicionar orderby
cláusula, para certificar-se de Top(10)
retorna correto dez itens.
Outras dicas
Este é o teste, mas acredito que a sintaxe deve trabalhar para um lambda de consulta.Como você se juntar mais tabelas com esta sintaxe, você precisa de fazer mais para dentro da nova objetos para alcançar os valores que você deseja manipular.
var fullEntries = dbContext.tbl_EntryPoint
.Join(
dbContext.tbl_Entry,
entryPoint => entryPoint.EID,
entry => entry.EID,
(entryPoint, entry) => new { entryPoint, entry }
)
.Join(
dbContext.tbl_Title,
combinedEntry => combinedEntry.entry.TID,
title => title.TID,
(combinedEntry, title) => new
{
UID = combinedEntry.entry.OwnerUID,
TID = combinedEntry.entry.TID,
EID = combinedEntry.entryPoint.EID,
Title = title.Title
}
)
.Where(fullEntry => fullEntry.UID == user.UID)
.Take(10);
var entryPoint = (from ep in dbContext.tbl_EntryPoint
join e in dbContext.tbl_Entry on ep.EID equals e.EID
join t in dbContext.tbl_Title on e.TID equals t.TID
where e.OwnerID == user.UID
select new {
UID = e.OwnerID,
TID = e.TID,
Title = t.Title,
EID = e.EID
}).Take(10);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow