Bringing other columns with GroupBy with LINQ

1

I have two Tables:

1st Process containing ProcessId and SituationProcessId

2nd SituationProcess containing StatusProcessId Description

Templates:

public class ProcessoModel
{
    public int? ProcessoId { get; set; }
    public int? SituacaoProcessoId { get; set; }
    public virtual SituacaoProcessoModel SituacaoProcesso { get; set; }

}

public class SituacaoProcessoModel
{
    public int? SituacaoProcessoId { get; set; }      
    public string Descricao { get; set; }
    public virtual List<ProcessoModel> LstProcesso { get; set; }
}

I want to bring the total count of processes for each type of situation, my code below is already doing this, but I want the description to come in the Description column of the SituationProcess table and not the SituationProcessoId, how can I do this? / p>

var processo = db.TbProcesso
            .GroupBy(p => p.SituacaoProcessoId)
            .Select(p => new
            {
                Descricao = p.Key,
                Total = p.Count()
            });

In my Sql I can, the problem is doing with LINQ

SELECT 
    sp.Descricao, COUNT(p.SituacaoProcessoId) AS Total
FROM
    processo p
        LEFT JOIN
    situacao_processo sp ON p.SituacaoProcessoId = sp.SituacaoProcessoId
GROUP BY p.SituacaoProcessoId

Result:

    
asked by anonymous 06.09.2017 / 16:08

1 answer

1

Just make use of the navigation properties.

var processo = Processos
                .GroupBy(p => new 
                             { 
                                 p.SituacaoProcessoId, 
                                 Descr = p.SituacaoProcesso.Descricao 
                              })
                .Select(p => new
                {
                    Descricao = p.Key.Descr,
                    Total = p.Count()
                });
    
06.09.2017 / 16:38