Dynamic Expression API: I can do a predicate, how to code an OrderBy Specification?
I figured out how to do a predicate from a string supplied by a client based on Dynamic Linq (this is wrapped in a Specification object):
return System.Linq.Dynamic.DynamicExpression.ParseLambda<开发者_StackOverflow社区;TE, bool>
(filter.ToString(), arguments.ToArray())
where filter is a string of expression language as described in their help file. Works like a charm.
However, is it possible to translate a string list of orderby fields into a strongly typed expression like this:
Expression<Func<E, object>> orderby
Notes:
Here's my repository method to filter and order items
public IList<E> Get(Expression<Func<E, bool>> filterLambda = null,
Expression<Func<E, object>> orderbyLambda = null,
int? page = null,
int? pageSize = null)
I would like to call it using:
var a = Repo.Get( filterLambda: Specification.Where( StringListOfFilters),
orderbyLambda: Specification.OrderBy( StringListOfOrderBy),
page: 1,
pageSize: 100 );
Any ideas on how to convert a string list of OrderBy fields into a strongly typed lambda expression? Examples of the strings would be:
fieldname
fieldname descending
Another note:
I am translating the strings coming from the client, which are fieldname op value
validating them with Regex to prevent injection. Also, I am only referencing Dynamic Linq in the Specification object, not the repository.
This is probably not a problem anymore.. however it looks like your solution is given here.
精彩评论