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:
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????
– WPfan
And the second way you quote is not possible, because I have to go through the foreach anyway!!
– WPfan
Look at the error, I edited the question with the attachment
– WPfan
@Wpfan tries to give put like this
dashboard.NotasFiscais = lista.ToList();
– Filipe Oliveira
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
@Wpfan which error gives?
– Filipe Oliveira
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
@Wpfan has how to put in question your entities? I thought in the mapping Participant had a list of Notafiscal.
– Filipe Oliveira
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
@Wpfan understand, so have to do the opposite way even using the invoice. When and have a while, I try to do here
– Filipe Oliveira
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?!
– WPfan