mardi 25 octobre 2016

Building OrderBy-expression at runtime by property-name which can be nested

How can I dynamically build an order-by expression when only knowing the name of the property (or even the name of a sub-property)?

What I'm trying to achieve is something like:

dbResult = // some database-query as IQueryable<TSource> which is not yet executed;

if (!string.IsNullOrEmpty(request.OrderBy)) { // the user want's to group the results
    var grouped = dbResult.GroupBy(/* this should be build dynamically */);
}

I need something to start with as GroupBy is awaiting a Func<TSource, TKey> but I only know TKey at runtime which can be string, int or even a Guid.

The user could pass something like "Country.Name" to the request.OrderBy property which means the results should be grouped by a sub-property (sub-select), the name of a country.

I think ExpressionTrees is the way to go here but I'm stuck before even getting started as I don't know how to handle the unknown Type as well as the option to group by a property of a sub-select/sub-property.





Aucun commentaire:

Enregistrer un commentaire