Something is missing me, and I'm not quite sure that I don't have much experience with LINQ expressions.
I am trying to change the following code snippet in an expression.
MethodInfo orderByMethod = (from method in typeof(QueryableExtensions).GetMethods() where method.Name == "OrderBy" && method.GetGenericArguments().Length > 0 select method).First(); MethodInfo orderByGenericMethod = orderByMethod.MakeGenericMethod(new[] { queryable.ElementType }); IQueryable queryable = orderByGenericMethod.Invoke(null, new object[] { queryable, sorting.ColumnName, sorting.Direction }) as IQueryable;
Here is my attempt.
Expression orderByMethodExpression = Expression.Call(typeof(QueryableExtensions), "OrderBy", new[] { queryable.ElementType }, Expression.Constant(queryable), Expression.Constant(sorting.ColumnName), Expression.Constant(sorting.Direction)); IQueryable queryable = queryable.Provider.CreateQuery(orderByMethodExpression)
The corresponding code.
SortingExpression sorting = SortingExpression.Create(arguments.SortExpression); IQueryable queryable = enumerable.AsQueryable(); if (sorting != null) { MethodInfo orderByMethod = (from method in typeof(QueryableExtensions).GetMethods() where method.Name == "OrderBy" && method.GetGenericArguments().Length > 0 select method).First(); MethodInfo orderByGenericMethod = orderByMethod.MakeGenericMethod(new[] { queryable.ElementType }); queryable = orderByGenericMethod.Invoke(null, new object[] { queryable, sorting.ColumnName, sorting.Direction }) as IQueryable; } object[] items = Enumerable.Cast<object>(queryable).ToArray(); arguments.TotalRowCount = items.Length; enumerable = items;
The error I am getting.
There is no general "OrderBy" method in the type "Shilony.Web.UI.WebControls.QueryableExtensions" is compatible with the arguments and arguments of the supplied type. No type arguments should be provided unless the method is general.
Just to clarify OrderBy, my own extension method here is the method signature.
public static IQueryable<TSource> OrderBy<TSource>(this IQueryable<TSource> source, string propertyName, string direction) where TSource : class
Just to understand what's wrong, I changed it to this.
MethodInfo orderByMethod = (from method in typeof(QueryableExtensions).GetMethods() where method.Name == "OrderBy" && method.GetGenericArguments().Length > 0 select method).First(); MethodInfo orderByGenericMethod = orderByMethod.MakeGenericMethod(new[] { queryable.ElementType }); Expression orderByMethodExpression = Expression.Call(orderByGenericMethod, Expression.Constant(queryable, typeof(IQueryable)), Expression.Constant(sorting.ColumnName, typeof(string)), Expression.Constant(sorting.Direction, typeof(string))); queryable = queryable.Provider.CreateQuery(orderByMethodExpression);
And now I get this error.
An expression of type "System.Linq.IQueryable" cannot be used for a parameter of type "System.Linq.IQueryable 1[Shilony.DomainLayer.DomainObjects.Customer]' of method 'System.Linq.IQueryable 1 [Shilony.DomainLayer.DomainObjects.Customer] OrderBy [Customer] (System.Linq.IQueryable`) 1 [Shilony.DomainLayer.DomainObjects.Customer], System.String, System.String) '
I do not get it when I call it with the same arguments as it works, but when I try to turn it all into an expression, it will not work.