Error is
LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object, System.Object[])' method, and this method cannot be translated into a store expression.
My code is
public static GridResult GetAllUsers(int count, int tblsize,string sortcreteria)
{
using (UserEntities entity = new UserEntities())
{
var data = entity.User_Details.Take(count)
.OrderBy(i =>.GetType().GetProperty(sortcreteria).GetValue(i,null))
.Skip(tblsize).ToList();
result.DataSource = data;
result.Count = entity.User_Details.Count();
}
return result;
}
How to sort with property name as string?
You will probably need to use
Expression Trees
to construct the Linq statementOrderBy(x => x.SomeProperty)
.Create an OrderBy Expression for LINQ/Lambda
Create LINQ to entities OrderBy expression on the fly
You could try to do this using the (somewhat old) Dynamic LINQ library:
Alternatively, you can still sort the sequence using your original query by moving the objects into memory first, since the LINQ to Entities provider can't translate calls to the Reflection API into SQL:
Just add the following extension to your code and you're good to go:
}
Usage:
Edit:
For support for the
ThenBy
method however the following methods returning anIOrderedQueryable
should take care of it all: