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 8153259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:57:36+00:00 2026-06-06T15:57:36+00:00

I use this piece of code (found on stackoverflow) to generate an OrderBy static

  • 0

I use this piece of code (found on stackoverflow) to generate an “OrderBy”

static class BuilderOrder
{
    public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
    {
        return ApplyOrder<T>(source, property, "OrderBy");
    }

    public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
    {
        return ApplyOrder<T>(source, property, "OrderByDescending");
    }

    public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
    {
        return ApplyOrder<T>(source, property, "ThenBy");
    }

    public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
    {
        return ApplyOrder<T>(source, property, "ThenByDescending");
    }

    static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
    {
        string[] props = property.Split('.');
        Type type = typeof(T);
        ParameterExpression arg = Expression.Parameter(type, "x");
        Expression expr = arg;

        foreach (string prop in props)
        {
            // use reflection (not ComponentModel) to mirror LINQ
            PropertyInfo pi = type.GetProperty(prop);
            expr = Expression.Property(expr, pi);
            type = pi.PropertyType;
        }

        Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
        LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);

        object result = typeof(Queryable).GetMethods().Single(
                method => method.Name == methodName
                        && method.IsGenericMethodDefinition
                        && method.GetGenericArguments().Length == 2
                        && method.GetParameters().Length == 2)
                .MakeGenericMethod(typeof(T), type)
                .Invoke(null, new object[] { source, lambda });
        return (IOrderedQueryable<T>)result;
    }
}

I have this object :

public class Person : IPerson
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public interface IPerson
{
    int Id { get; set; }
    string FirstName { get; set; }
    string LastName { get; set; }
}

It’s use like this

private void Tri(IQueryable<IPerson> list, string field, string direction)
{
    if (direction.ToUpper() == "DESC")
    {
        return BuilderOrder.OrderByDescending(liste, field);
    }
    else
    {
        return BuilderOrder.OrderBy(liste, field);
    }
}

The problem, the sorting is not working.
I’d like a way to send as parameter, the list to sort IQueryable<IPerson>, better IQueryable<T>, the fieldname (for sorting) and the direction (asc/desc)

Any idea ?

Thanks,

  • 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-06-06T15:57:38+00:00Added an answer on June 6, 2026 at 3:57 pm

    I can believe it’s not working… as you work in a void Method, without ref parameter !

    Put your method Tri (rename it to SortBy or some English name would be better) in the BuilderOrder class and

    public static IOrderedQueryable<T> SortBy(this IQueryable<T> queryable, string field, string direction)
    {
        return (direction.ToUpper() == "DESC") 
           ? queryable.OrderByDescending(field)
           : queryable.OrderBy(field);
    }
    

    and you could add another one :

    public static IOrderedQueryable<T> SortThenBy(this IOrderedQueryable<T> orderedQueryable, string field, string direction)
    {
        return (direction.ToUpper() == "DESC") 
           ? orderedQueryable.ThenByDescending(field)
           : orderedQueryable.ThenBy(field);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build multilangual site. I use this piece of code to detect
A common piece of code I use for simple string splitting looks like this:
I wrote this piece of code to my bootstrap public function _initRouter() { $pages
We have the following piece of code (idea for this code was found on
I'm using this piece of code I found ( http://impnerd.com/wordpress-hack-add-post-images-to-your-homepage ) to display the
In Cprogramming.com I found this piece of code: int a,b; scanf(%d %d,&a,&b); b=(a+b)-(a=b); printf(%d
While browsing questions and answers in this forum i found a piece of code
I am trying to use this piece of code to serialize a form AND
I found this piece of Java code at Wikipedia that is supposed to shuffle
I found this piece of code in perl system(zip $ZIP_DEBUG -r -9 itvlib.zip $include

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.