Entity Framework Query with Inner Join and Where

Asked

Viewed 9,692 times

2

I am making a query using Inner Join of some tables and a Where with only one condition, but the data is not returned in EF,the same query in Mysql works and returns data.

Follows the code:

string raAluno = acr.getAlunoLogado().ToString();
            var aluno = (from al in neEAD.mot_aluno
                               join ta in neEAD.mot_turmaaluno on al.al_id equals ta.ta_al_id
                               join tm in neEAD.mot_turma on ta.ta_tm_id equals tm.tm_id
                               join mo in neEAD.mot_modulo on tm.tm_mod_id equals mo.mod_id
                               join dp in neEAD.mot_disciplina on mo.mod_id equals dp.dp_mod_id
                               where (al.al_ra == "'" + raAluno + "'")
                               select new IndexModel
                               {
                             ID = al.al_id,
                             Nome = al.al_nome,
                             Disciplina = dp.dp_descricao,
                             IDDisciplina = dp.dp_id,
                         }).ToString();
            return View(aluno);

Can anyone help me with this, because I can’t see the sql query to check if it is correct.

2 answers

3


Utilize ToList() instead of ToString() or even withdraw this method.

The LINQ returns a IEnumerable / IQueryable, and not a string.

Introduction to LINQ queries (C#)

var aluno = (from al in neEAD.mot_aluno
                   join ta in neEAD.mot_turmaaluno on al.al_id equals ta.ta_al_id
                   join tm in neEAD.mot_turma on ta.ta_tm_id equals tm.tm_id
                   join mo in neEAD.mot_modulo on tm.tm_mod_id equals mo.mod_id
                   join dp in neEAD.mot_disciplina on mo.mod_id equals dp.dp_mod_id
                   where (al.al_ra == "'" + raAluno + "'")
                   select new IndexModel
                   {
                 ID = al.al_id,
                 Nome = al.al_nome,
                 Disciplina = dp.dp_descricao,
                 IDDisciplina = dp.dp_id,
             }).ToList();
  • 1

    I switched to . Tolist() and removed from Where the single and double quotes, so it worked correctly.

2

Perhaps your problem is the simple quotation marks in the comparison and the Tostring on the return of the Ring, where I imagine you want the first object returned. Try with the code below:

string raAluno = acr.getAlunoLogado().ToString();
        var aluno = (from al in neEAD.mot_aluno
                           join ta in neEAD.mot_turmaaluno on al.al_id equals ta.ta_al_id
                           join tm in neEAD.mot_turma on ta.ta_tm_id equals tm.tm_id
                           join mo in neEAD.mot_modulo on tm.tm_mod_id equals mo.mod_id
                           join dp in neEAD.mot_disciplina on mo.mod_id equals dp.dp_mod_id
                           where (al.al_ra == raAluno)
                           select new IndexModel
                           {
                         ID = al.al_id,
                         Nome = al.al_nome,
                         Disciplina = dp.dp_descricao,
                         IDDisciplina = dp.dp_id,
                     }).FirstOrDefault();
        return View(aluno);

Browser other questions tagged

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