Try this ... (but I'm not sure if he will do what you want.)
public static class Extensions
{
public static IQueryable<T> Sort<T>(this IQueryable<T> query,
string sortField,
SortDirection direction)
{
if (direction == SortDirection.Ascending)
return query.OrderBy(s => s.GetType()
.GetProperty(sortField));
return query.OrderByDescending(s => s.GetType()
.GetProperty(sortField));
}
}
... The general parameter should be in the method, not in the class. Moving from Extensions<T>to Sort<T>(allows you to get rid of your compiler error.
, , PropertyInfo orderby. , , , . LINQ.
... Dynamic LINQ.
public static IQueryable OrderBy(this IQueryable source,
string ordering,
params object[] values) {
if (source == null) throw new ArgumentNullException("source");
if (ordering == null) throw new ArgumentNullException("ordering");
ParameterExpression[] parameters = new ParameterExpression[] {
Expression.Parameter(source.ElementType, "") };
ExpressionParser parser = new ExpressionParser(parameters,
ordering,
values);
IEnumerable<DynamicOrdering> orderings = parser.ParseOrdering();
Expression queryExpr = source.Expression;
string methodAsc = "OrderBy";
string methodDesc = "OrderByDescending";
foreach (DynamicOrdering o in orderings) {
queryExpr = Expression.Call(
typeof(Queryable), o.Ascending ? methodAsc : methodDesc,
new Type[] { source.ElementType, o.Selector.Type },
queryExpr, Expression.Quote(Expression.Lambda(o.Selector,
parameters)));
methodAsc = "ThenBy";
methodDesc = "ThenByDescending";
}
return source.Provider.CreateQuery(queryExpr);
}