(p) => p.Address.Street "...">

How to use a string to create an EF order by expression?

I am trying to achieve this conversion

"Address.Street" => (p) => p.Address.Street
"Name" => (p) => p.Name

I managed to find a way to generate an order using reflection, but it will not work for complex sorting as Address.Streetit works for a single level property.

Is there any way to do this? I saw that I am compiling lambda expressions, but I could not figure out how to make it work in this case.

+2
source share
1 answer

Creating an expression is not difficult, but the difficult part is binding it to the corresponding methods OrderBy(Descending)/ ThenBy(Descendig)when you do not know the type of the property (therefore, the type of the result of the selector expression).

, :

public static partial class QueryableExtensions
{
    public static IOrderedQueryable<T> OrderByMember<T>(this IQueryable<T> source, string memberPath)
    {
        return source.OrderByMemberUsing(memberPath, "OrderBy");
    }
    public static IOrderedQueryable<T> OrderByMemberDescending<T>(this IQueryable<T> source, string memberPath)
    {
        return source.OrderByMemberUsing(memberPath, "OrderByDescending");
    }
    public static IOrderedQueryable<T> ThenByMember<T>(this IOrderedQueryable<T> source, string memberPath)
    {
        return source.OrderByMemberUsing(memberPath, "ThenBy");
    }
    public static IOrderedQueryable<T> ThenByMemberDescending<T>(this IOrderedQueryable<T> source, string memberPath)
    {
        return source.OrderByMemberUsing(memberPath, "ThenByDescending");
    }
    private static IOrderedQueryable<T> OrderByMemberUsing<T>(this IQueryable<T> source, string memberPath, string method)
    {
        var parameter = Expression.Parameter(typeof(T), "item");
        var member = memberPath.Split('.')
            .Aggregate((Expression)parameter, Expression.PropertyOrField);
        var keySelector = Expression.Lambda(member, parameter);
        var methodCall = Expression.Call(
            typeof(Queryable), method, new[] { parameter.Type, member.Type },
            source.Expression, Expression.Quote(keySelector));
        return (IOrderedQueryable<T>)source.Provider.CreateQuery(methodCall);
    }
+10

All Articles