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<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
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
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.