1

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

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.

Zachary Scott
  • 20,968
  • 35
  • 123
  • 205

1 Answers1

0

This is probably not a problem anymore.. however it looks like your solution is given here.

Community
  • 1
  • 1
Jonno
  • 1,976
  • 2
  • 21
  • 21