Can someone explain the syntax for building an Expression that will OrderBy a user-specified property on an entity?
This MSDN article goes a long way to helping, but it deals with a simple list of strings, my data set contains my own custom objects.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Code first, explanation later.
So, first we get hold of the data as a Queryable object. This has a kind of ‘root’ Expression property, and we need that.
The eachItem thing is an expression that represents the argument placeholder in a Lambda, the symbol in the goes to, if you will.
Then we make an expression that does the read operation the property name specified by the user in propertyName.
We finally build an expression which does the call to the OrderBy method on the Queryable data. We’re saying (in order of argument):
The last two are in { } since its actually a param array. I used IComparable since the property could be any type but obviously needs to be comparable to be ordered.
Luke