Group by with linq

4

I have the following tables:

**Aula**
Id
Nome
Data

**Avaliacao**
Aula_Id
Aluno_Id
Organizacao
Didatica

To get a join of the tables I use the code:

var result = from a in mdc.SBE_AA_Aulas
             join av in mdc.SBE_AA_AvaliacaoAlunos on a.id equals av.SBE_AA_Aula_id

             select new
             {
               a,
               av
             };

Where Organization and Didatica are the notes.

I would like a consultation through linq, which would bring an average of Organization and Didatica grades for each lesson.

But I can not do that.

    
asked by anonymous 04.06.2014 / 19:33

1 answer

5

That works, but I'm not sure if it's the best way:

var result = from a in mdc.SBE_AA_Aulas
            join av in mdc.SBE_AA_AvaliacaoAlunos on a.id equals av.SBE_AA_Aula_id
            group new {a, av} by av.Aula_Id into grp
            select new {
                NomeAula = grp.Select(x => x.a.Nome).FirstOrDefault(),
                MediaOrganizacao = grp.Average(x => x.av.Organizacao),
                MediaDidatica = grp.Average(x => x.av.Didatica)
            }
    
04.06.2014 / 20:42