mercredi 16 décembre 2015

Dynamic linq OrderBy() failing after adding HasValue in the expression

I had followed this link to create dynamic orderby clause in linq queries using Expression trees.

But later I came across a scenario where I had to sort the values of a column with keeping the null value in mind as my database column allows null i.e. All the non-null values should be sorted ascending and then null values should be followed.

So I followed this link to modify my dynamic linq code to handle the above scenario. i.e. I tried to add "HasValue" call in my OrderBy() call. But it started to fail with below error.

No generic method 'OrderBy' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.

The argument that causes Expression.Call() to fail: p.IsProcessStarted.HasValue

Below is the code that I am using:

public static class ExtensionMethods
    {
        public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> source, string orderByProperty,
                          bool desc)
        {
            string command = desc ? "OrderByDescending" : "OrderBy";
            var type = typeof(TEntity);
            var property = type.GetProperty(orderByProperty);
            var propertyType = property.PropertyType;
            bool isNullable = propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>);

            var parameter = Expression.Parameter(type, "p");
            MemberExpression propertyAccess = Expression.MakeMemberAccess(parameter, property);
            MemberExpression hasValue = Expression.Property(propertyAccess, "HasValue");            
            LambdaExpression orderByExpression = Expression.Lambda(hasValue, parameter);
            MethodCallExpression resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType },
                                          source.Expression, Expression.Quote(orderByExpression));
            return source.Provider.CreateQuery<TEntity>(resultExpression);
        }
}





Aucun commentaire:

Enregistrer un commentaire