IQueryable <T> Extension Method Does Not Work

How can I create an extension method that will work as follows

public static class Extensions<T>
{
    public static IQueryable<T> Sort(this IQueryable<T> query, string sortField, SortDirection direction)
    {
        // System.Type dataSourceType = query.GetType();

        //System.Type dataItemType = typeof(object);

        //if (dataSourceType.HasElementType)
        //{
        //    dataItemType = dataSourceType.GetElementType();
        //}
        //else if (dataSourceType.IsGenericType)
        //{
        //    dataItemType = dataSourceType.GetGenericArguments()[0];
        //}

        //var fieldType = dataItemType.GetProperty(sortField);
        if (direction == SortDirection.Ascending)
            return query.OrderBy(s => s.GetType().GetProperty(sortField));
        return query.OrderByDescending(s => s.GetType().GetProperty(sortField));

    }
}

It is currently said that "extension methods must be defined in a nonequivalent static class."

How should I do it?

+3
source share
3 answers

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);
}
+13

:

public static class Extensions
{
    public static IQueryable<T> Sort<T>(this IQueryable<T> query, string sortField, SortDirection direction)
    {
      //code
    }
}

, :)

+8

:

.

.

public static class Extensions // no <T>
{
    // ...
}
+3

Source: https://habr.com/ru/post/1739505/


All Articles