Join for 2 tables

Asked

Viewed 61 times

1

How do I use the Join method to give a include of a value from another table? In this case I was going to use the Chamadamusicas table and the Musicavotadas table

var chamadaMusicas = db.ChamadaMusicas
                       .Include(c => c.Chamada)
                       .Include(c => c.Musica)
                       .Where(c => c.Chamada.PessoaID.Equals(id)).Where(i => i.Chamada.Ativa.Equals(true))
                       .Join(db.MusicaVotadas)
                       .ToList();
  • What’s the matter?

  • This Join doesn’t work. It gives an error when I use this Join. So I must be using it wrong.

  • Put the bug. A drawing showing the relationship between the classes will help in understanding the problem.

1 answer

2


In case your John is spelled wrong. Follow below as it would be +- the correct form, depending on how your entities are:

var chamadaMusicas = db.ChamadaMusicas
   .Include(c => c.Chamada)
   .Include(c => c.Musica)
   .Where(c => c.Chamada.PessoaID.Equals(id)).Where(i => i.Chamada.Ativa.Equals(true))
   .Join(db.MusicaVotadas,
        ChamadaMusicas => ID, //Informa a PK (primeira parte da clausula on do sql)
        MusicaVotadas => ChamadaMusicasID //Informa a FK(segunda parte da clausula on do sql)
        (ChamadaMusicas, MusicaVotadas) = > new {ChamadaMusicas = ChamadaMusicas, MusicaVotadas = MusicaVotadas}// novo objeto formado pelo join
   ).ToList();

Here is also a link explaining how Join works

http://theburningmonk.com/2010/02/linq-lambda-expression-vs-query-expression/

Browser other questions tagged

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