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

0

Well, I'm developing the dashboard of a web system, in one of the filters I should list the top 5 clients and sort them by the highest amount of totals of invoices issued. I'm already able to list quietly, the problem is that the filter is listing everything (when it should just list 5) and it's messed up. Here is 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 anyone point me to the solution so that this filter lists only the 5 records with the largest amounts of invoices?

Below Jean and Philip gave me alternatives, after doing the suggested in both cases the following error occurs, see the image:

    
asked by anonymous 24.05.2018 / 20:37

2 answers

2

The filter was done but was not assigned again where it should be

You can do this:

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

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

Here's an example of how to stay:

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);
    
24.05.2018 / 21:17
2

Explanation

The problem with your code that does not sort as expected is as follows:

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

We can see that prior to sorting, you select five records using the Take(5) method. To work correctly, change the order to the one exemplified 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);
            }

For this:

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 at Ideone

    
24.05.2018 / 21:46