Query Entity Framework with Inner Join and Where

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.

Follow 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);

Someone can help me with this, because I can not see the sql query to see if it is correct.

    
asked by anonymous 21.05.2016 / 05:45

2 answers

3

Use ToList() instead of ToString() or even remove this method .

LINQ returns IEnumerable / IQueryable , not string .

Introduction to LINQ (C #) Queries

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();
    
21.05.2016 / 06:05
2

Maybe your problem is the single quotation marks in the comparison and the ToString in linq return, where I imagine you want the first object returned. Please try the following 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,
                     }).FirstOrDefault();
        return View(aluno);
    
21.05.2016 / 06:48