How can I write an OrderBy Specification using the Dynamic Expression API?

c# dynamic-linq lambda repository specification-pattern


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

7/28/2011 3:02:16 PM

Popular Answer

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

5/23/2017 12:11:25 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow