How do I put a Count in a string that returns a list

Asked

Viewed 196 times

1

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

Look at my screen: inserir a descrição da imagem aqui

See my letter below:

public static List<MontaArvoreAcao> CriarListaArvorePdv()
{
    RupturaEntities db = new RupturaEntities();

    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
       }
     ).Distinct().ToList().OrderBy(r => r.Descricao);

    return _listaPdv.ToList();
}
  • You installed the package Morelinq that I told you in the other answer? You don’t need to use this Count.

  • already tried to add a property in Montaarvoreacao and associate the value gr. Count?

  • I installed Morelinq. As I do now?

1 answer

1


I solved it like this. I created a field in my Model Montaarvoreacao, called Somatorio. Then in Brasili I did:

Somatorio = gr. Count()

My breath:

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 on 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>

Browser other questions tagged

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