I am having trouble adding an orderby clause to this link query.
- var purchaseOrderData = await _context.PurchaseOrders
- .Where(s => s.SupplierNumber == "86087319")
- .GroupBy(o => new
- {
- SupplierNumber = o.SupplierNumber,
- SupplierName = o.Supplier.SupplierName,
- Month = o.OrderDate.Month,
- Year = o.OrderDate.Year
- })
- .Select(g => new
- {
- SupplierNumber = g.Key.SupplierNumber,
- SupplierName = g.Key.SupplierName,
- MonthOrd = g.Key.Month,
- Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(g.Key.Month),
- Year = g.Key.Year,
- Count = g.Count(),
- OrderAmount= g.Sum(o => o.OrderTotal)
- })
- .ToListAsync();
The above returns
[
{
"supplierNumber": "86087319",
"supplierName": "SUPPLIER 1",
"monthOrd": 2,
"month": "February",
"year": 2019,
"count": 5,
"orderAmount": 414402.86
},
{
"supplierNumber": "86087319",
"supplierName": "SUPPLIER 2",
"monthOrd": 3,
"month": "March",
"year": 2019,
"count": 19,
"orderAmount": 190563.24
},....]
I am trying to order this list by year and then month (number) by adding
.OrderBy(a => a.Year)
.ThenBy(a => a.Month)
before the .ToListAsync();
But the query returns an error indicating that the query cannot be translated.
Can you please help me add the orderby clauses to the above linq query?
Thanks for your help.