TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Michael Seery
NA
85
10.5k
OrderBy when Grouping
May 21 2020 10:31 PM
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.
Reply
Answers (
13
)
The Entity or Complex Type cannot be constructed
Select boolean value into string in line query