First, I’m grabbing ClientID. Then, I get all Invoices associated with that ClientID. I want to return data all ordered by InvoiceNumber, descending. Here’s my code:
var rvInvoices =
(from i in db.QB_INVOICES_HEADER
where i.ClientID == cId
select i).ToList();
foreach (var itm in rvInvoices)
{
InvoiceModel cm = new InvoiceModel()
{
InvoiceNumber = itm.InvoiceNumber,
InvoiceSentDt = itm.InvoiceSentDt,
InvoiceDt = itm.InvoiceDt,
Amount = itm.Amount,
Term = itm.Term,
ClientName = itm.CI_CLIENTLIST.ClientName
};
listInvoices.Add(cm);
}
return listInvoices;
You can do the order in three places.
foreach, orreturnOption 1:
Option 2:
foreach (var itm in rvInvoices.OrderByDescending(i => i.InvoiceNumber))Option 3:
return listInvoices.OrderByDescending(i => i.InvoiceNumber).ToList();I would suggest taking route 1 since it will run the order at the database level.