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

Ask A Question

Stats

  • Questions 390k
  • Answers 390k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer By default, the below keys have "readonly" access for standard… May 15, 2026 at 1:04 am
  • Editorial Team
    Editorial Team added an answer David's comment is right. How could you ever determine that… May 15, 2026 at 1:04 am
  • Editorial Team
    Editorial Team added an answer Apply the following CSS: html { height: 100%; } body… May 15, 2026 at 1:04 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.