How do I put a count in a linq that returns a list

1

I need to make a count , for the following situation. See the image below, which I have repeated some pharmacies, see their CNPJ (06626253003681 and 06626253001476 ). They are repeated because they are on different dates. What I need is that I make a count in the number of pharmacies and the sum I put in the node Eucerin Hyaluron Night 50mg , that in this example would be 6 and next to each medicine the amount 1. It happens that in my LINQ I have a ToList() and this does not allow me to give Count() . I have a hard time doing this.

See how my screen is:

Seemylinkbelow:

publicstaticList<MontaArvoreAcao>CriarListaArvorePdv(){RupturaEntitiesdb=newRupturaEntities();var_listaPdv=(fromrindb.Rupturajoinaindb.Apresentacaoonr.Codigo_Apresentacaoequals(a.Codigo_Apresentacao)joinmindb.Motivoonr.IDMotivoequals(m.IDMotivo)joinpindb.PDVonr.CodigoPDVequals(p.CodigoPDV)wherer.IDMotivo!=6grouprbynew{p.Cnpj,loja=p.Descricao,a.Descricao,a.Familia,a.Unidade_Negocio,r.IDMotivo,r.DataRuptura}intogrselectnewMontaArvoreAcao{CnpjDescricao=gr.Key.Cnpj+" - " + gr.Key.loja,
           Descricao = gr.Key.Descricao,
           DataRuptura = gr.Key.DataRuptura,
           Familia = gr.Key.Familia,
           IDMotivo = gr.Key.IDMotivo,
           Unidade_Negocio = gr.Key.Unidade_Negocio
       }
     ).Distinct().ToList().OrderBy(r => r.Descricao);

    return _listaPdv.ToList();
}
    
asked by anonymous 24.09.2014 / 20:43

1 answer

1

I solved this. I created a field in my ModelManager Model, called Summarize. Then in linq I did:

Summation = gr.Count ()

My linq:

var _listaPdv = (
                                   from r in db.Ruptura
                                   join a in db.Apresentacao on r.Codigo_Apresentacao equals (a.Codigo_Apresentacao)
                                   join m in db.Motivo on r.IDMotivo equals (m.IDMotivo)
                                   join p in db.PDV on r.CodigoPDV equals(p.CodigoPDV)
                                   where r.IDMotivo != 6
                                   group r by new { p.Cnpj, loja = p.Descricao, a.Descricao, a.Familia, a.Unidade_Negocio, r.IDMotivo, r.DataRuptura } into gr

                                   select new MontaArvoreAcao
                                   {
                                       CnpjDescricao = gr.Key.Cnpj + " - " + gr.Key.loja,
                                       Descricao = gr.Key.Descricao,
                                       DataRuptura = gr.Key.DataRuptura,
                                       Familia = gr.Key.Familia,
                                       IDMotivo = gr.Key.IDMotivo,
                                       Unidade_Negocio = gr.Key.Unidade_Negocio,
                                       Somatorio = gr.Count()
                                   }
                                 ).Distinct().ToList().OrderBy(r => r.Descricao);

And my call in View:

<ul>
                                                                            @foreach (var pdv in (List<Ruptura.Models.MontaArvoreAcao>) ViewData["ListaPdv"])
                                                                            {
                                                                                if (@pdv.Descricao == @prod.Descricao && @pdv.IDMotivo == @item.IDMotivo)
                                                                               {
                                                                                        <li item-checked='false' item-expanded='false'>
                                                                                            @pdv.CnpjDescricao (@pdv.Somatorio)

                                                                                            </li>
                                                                            }
                                                                            }
                                                                        </ul>
    
25.09.2014 / 21:38