IQueryable<T> Extension Method not working

Posted by Micah on Stack Overflow See other posts from Stack Overflow or by Micah
Published on 2010-04-01T18:32:35Z Indexed on 2010/04/01 18:43 UTC
Read the original article Hit count: 1145

Filed under:
|
|

How can i make an extension method that will work like this

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));

    }
}

Currently that says "Extension methods must be defined in a non-generic static class".

How do i do this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ