I am trying to do a dynamic order by on columns using Marc Gravell's code. I am posting the 2 queries. It works in one case but doesn't work in 2nd case. Can anybody tell me what changes I need to make to make both queries run perfectly?
This is the link to the Marc Gravell's answer:
https://stackoverflow.com/a/233505
I am using Northwind database. These are both my queries:
var query = (from cust in northwindEntities.Customers
select new
{
City = cust.City ,
Orders = northwindEntities.Orders
.Where(o => o.CustomerID == cust.CustomerID)
.OrderBy("OrderID")
}); // doesn't work.
var query = (from cust in northwindEntities.Customers
select new
{
City = cust.City ,
//Orders = northwindEntities.Orders.Where(o => o.CustomerID == cust.CustomerID).
// OrderBy("OrderID")
}).OrderBy("City"); // works
Here is the exception of the 1st query:
LINQ to Entities does not recognize the method 'System.Linq.IOrderedQueryable
1[ConsoleApplication12.Order] OrderBy[Order](System.Linq.IQueryable
1[ConsoleApplication12.Order], System.String)' method, and this method cannot be translated into a store expression.