How to sort and list only 5 items from a list?

Asked

Viewed 250 times

0

You see, I am developing the Dashboard of a web system, in one of the filters I should list the 5 main customers and sort them by higher value of the sums of total of issued bills. I’m already able to quietly list, the problem is that the filter is listing everything (when it should just list 5) and is disorderly. Follow my code below:

public ActionResult Index()

        {
            var dashboard = new DashboardViewModel();


                var lista = new List<NotaFiscal>();


            var participantes = db.Participantes
                          .ToList();
            var notas = db.NotasFiscais
                          .ToList();

            foreach (var part in participantes)
            {
                var x = new NotaFiscal();
                var res = notas.Where(y => y.ClienteID == part.ParticipanteId).Sum(o => o.ValorTotalNota);
                x.ClienteID = part.ParticipanteId;
                x.ValorTotalNota = res;
                x.NomeCliente = part.NomeParticipante;
                lista.Add(x);
            }


            dashboard.NotasFiscais = lista;
            dashboard.NotasFiscais.Take(5).OrderByDescending(x => x.ValorTotalNota);
            return View(dashboard);
        }

Would someone point out to me the solution so that this filter lists only the 5 records with the highest amounts of bills?

Below Jean and Filipe gave me alternatives, after making the suggested in both cases occur the following error, see the image: inserir a descrição da imagem aqui

2 answers

2

The filter was made but was not assigned again where it should

You can do it like this:

dashboard.NotasFiscais = lista.Take(5).OrderByDescending(x => x.ValorTotalNota);
return View(dashboard);

And you can change the code a little because of the queries, since when a toList is done, the query is executed and this can affect the performance of what you are implementing.

Here’s an example of what it might look like:

var dashboard = new DashboardViewModel();


var lista = dbParticipantes
    .Select(e => new NotaFiscal {  
        ClienteID = e.ParticipanteId,
        ValorTotalNota = e.NotasFiscais.Sum(n => n.ValorTotalNota)
    })
    .OrderByDescending(e => e.ValorTotalNota)
    .Take(5);

dashboard.NotasFiscais = lista;
return View(dashboard);
  • So friend, thank you more as you pointed out I come across the following problems: It is not possible to convert implicitly type "System.Linq.Iorderedenumerable<Ebase.EmissorNFeWeb.Models.Notafiscal>" in "System.Collections.Generic.Icollection<Ebase.EmissorNFeWeb.Models.Notafiscal>". There is an explicit conversion (is there a missing conversion?) Dai how would I solve it now????

  • And the second way you quote is not possible, because I have to go through the foreach anyway!!

  • Look at the error, I edited the question with the attachment

  • 1

    @Wpfan tries to give put like this dashboard.NotasFiscais = lista.ToList();

  • Filipe, until it would work yes, but the function you made there contains an error, in this line I can not recover the Notasfiscais: ValorTotalNota = e.NotasFiscais.Sum(n => n.ValorTotalNota)

  • @Wpfan which error gives?

  • This error ai: "Participants" does not contain a definition for "Notasfiscais" and could not find any extension method "Notasfiscais" that accepts a first argument of type "Participants" (there is an missing Assembly reference or usage directive?)

  • @Wpfan has how to put in question your entities? I thought in the mapping Participant had a list of Notafiscal.

  • There’s no point in me passing you the entity, it has more than 20 fields and n has no Notasfiscais in it, got it?

  • @Wpfan understand, so have to do the opposite way even using the invoice. When and have a while, I try to do here

  • And oh Philip, can you demonstrate the example of the way you quoted in the last comment??? I wait to see if it goes right here?!

Show 6 more comments

2

Explanation

The problem of your code that does not sort as expected is in the section below:

 dashboard.NotasFiscais.Take(5).OrderByDescending(x => x.ValorTotalNota);

We can see that before sorting, you select five records through the method Take(5). To make it work properly, change the order to the example below:

dashboard.NotasFiscais.OrderByDescending(x => x.ValorTotalNota).Take(5);

Suggestion

Change this code block:

   foreach (var part in participantes)
            {
                var x = new NotaFiscal();
                var res = notas.Where(y => y.ClienteID == part.ParticipanteId).Sum(o => o.ValorTotalNota);
                x.ClienteID = part.ParticipanteId;
                x.ValorTotalNota = res;
                x.NomeCliente = part.NomeParticipante;
                lista.Add(x);
            }

East:

var lista = db.NotasFiscais
                 .Where(x => db.Participantes.Any(y => y.ParticipanteID == x.ClienteID))
                 .GroupBy(z => z.ClienteID)
                 .Select(x => new NotaFiscal(){
                        ClienteID = x.First().ClienteID,
                        ValorTotalNota = x.Sum(n => n.ValorTotalNota)
                  })
                 .OrderByDescending(x => x.ValorTotalNota)
                 .Take(5)
                 .ToList();

dashboard.NotasFiscais = lista;
return View(dashboard);

See working on Ideone

  • Jean, in my case I followed your example and I keep coming across the same problem reported earlier, when it is done 'Dashboard.Notesfiscais = list;' of It is not possible to convert implicitly type "System.Linq.Iorderedenumerable<Ebase.EmissorNFeWeb.Models.Notafiscal>" in "System.Collections.Generic.Icollection<Ebase.EmissorNFeWeb.Models.Notafiscal>". There is an explicit conversion (there is an absent conversion?)

  • And you cite that the problem of not ordering correctly is due to this here: Dashboard.NotasFiscais.Take(5). Orderbydescending(x => x.Valortotalnote); more not, because previously I had done the way you put it down and it was disordered in the same way

  • I edited the question with the bug attachment, you see, the way you suggested

  • I made a change, add a Tolist() to the end of the new code snippet.

Browser other questions tagged

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