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: