Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 703933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:53:08+00:00 2026-05-14T03:53:08+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:53:08+00:00Added an answer on May 14, 2026 at 3:53 am

    Try this… (but I’m not sure it 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 generic parameter should be on the method and not on the class. Moving it from Extensions<T> to Sort<T>( would allow you to get rid of the compiler error you are having.

    As for what you are trying to do with reflection, you would be returning a PropertyInfo object for the orderby clause. This is most likely not compable with the expression tree that you want. You may want to look at Dynamic LINQ.

    … this is an extract from 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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {
I wanted to write an extension-method that would work on dictionaries whose values were
from what dll can i get the extension of Intercept ? I've added Ninject.Extensions.Interception
There's a method in a library out of my control that uses a block
How can I deliver a module that requires patching flymake, with minimal startup time
I made a c extension out of a python script that was fairly labour
This is one of the hardest questions I've ever had to title here on
It's a pretty standard setup, WinForms client > Self-serving Business/Data layer > SQL Server
When dealing with an enumeration containing none, one or many of an item the
I am working on a client/server game (C#.NET) where I do not want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.