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.

Asma KhalidRank #160 · 14.7K reputationPosted May 21, 2020, 6:57 PM EDT
Asma KhalidRank #160 · 14.7K reputationPosted May 21, 2020, 7:48 PM EDT
Michael Seery81 reputationPosted May 21, 2020, 7:53 PM EDT
Michael Seery81 reputationPosted May 21, 2020, 7:15 PM EDT
>>' does not contain a definition for 'OrderBy' and no accessible extension method 'OrderBy' accepting a first argument of type 'Task
>>' could be found (are you missing a using directive or an assembly reference?)
Asma KhalidRank #160 · 14.7K reputationPosted May 21, 2020, 7:21 PM EDT
Michael Seery81 reputationPosted May 21, 2020, 7:25 PM EDT
Asma KhalidRank #160 · 14.7K reputationPosted May 21, 2020, 7:28 PM EDT
Michael Seery81 reputationPosted May 21, 2020, 7:37 PM EDT
Michael Seery81 reputationPosted May 21, 2020, 7:38 PM EDT
Michael Seery81 reputationPosted May 21, 2020, 7:45 PM EDT
Asma KhalidRank #160 · 14.7K reputationPosted May 21, 2020, 7:55 PM EDT
Michael Seery81 reputationPosted May 21, 2020, 8:01 PM EDT
Asma KhalidRank #160 · 14.7K reputationPosted May 21, 2020, 8:02 PM EDT