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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:21:47+00:00 2026-05-14T21:21:47+00:00

I’m attempting to perform dynamic sorting of data that I’m putting into grids into

  • 0

I’m attempting to perform dynamic sorting of data that I’m putting into grids into our MVC UI. Since MVC is abstracted from everything else via WCF, I’ve created a couple utility classes and extensions to help with this. The two most important things (slightly simplified) are as follows:

    public static IQueryable<TModel> ApplySortOptions<TModel, TProperty>(this IQueryable<TModel> collection, IEnumerable<ISortOption<TModel, TProperty>> sortOptions) where TModel : class
    {
        var sortedSortOptions = (from o in sortOptions
                                 orderby o.Priority ascending
                                 select o).ToList();

        var results = collection;

        foreach (var option in sortedSortOptions)
        {
            var currentOption = option;
            var propertyName = currentOption.Property.MemberWithoutInstance();
            var isAscending = currentOption.IsAscending;

            if (isAscending)
            {
                results = from r in results
                          orderby propertyName ascending 
                          select r;
            }
            else
            {
                results = from r in results
                          orderby propertyName descending 
                          select r;
            }
        }

        return results;
    }


public interface ISortOption<TModel, TProperty> where TModel : class
{
    Expression<Func<TModel, TProperty>> Property { get; set; }
    bool IsAscending { get; set; }
    int Priority { get; set; }
}

I’ve not given you the implementation for MemberWithoutInstance() but just trust me in that it returns the name of the property as a string. 🙂

Following is an example of how I would consume this (using a non-interesting, basic implementation of ISortOption<TModel, TProperty>):

var query = from b in CurrentContext.Businesses
            select b;

var sortOptions = new List<ISortOption<Business, object>>
                      {
                          new SortOption<Business, object>
                              {
                                  Property = (x => x.Name),
                                  IsAscending = true,
                                  Priority = 0
                              }
                      };

var results = query.ApplySortOptions(sortOptions);

As I discovered with this question, the problem is specific to my orderby propertyName ascending and orderby propertyName descending lines (everything else works great as far as I can tell). How can I do this in a dynamic/generic way that works properly?

  • 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-14T21:21:47+00:00Added an answer on May 14, 2026 at 9:21 pm

    While I think @tvanfosson’s solution will function perfectly, I’m also looking into this possibility:

        /// <summary>
        /// This extension method is used to help us apply ISortOptions to an IQueryable.
        /// </summary>
        /// <param name="collection">This is the IQueryable you wish to apply the ISortOptions to.</param>
        /// <param name="sortOptions">These are the ISortOptions you wish to have applied. You must specify at least one ISortOption (otherwise, don't call this method).</param>
        /// <returns>This returns an IQueryable object.</returns>
        /// <remarks>This extension method should honor deferred execution on the IQueryable that is passed in.</remarks>
        public static IOrderedQueryable<TModel> ApplySortOptions<TModel, TProperty>(this IQueryable<TModel> collection, IEnumerable<ISortOption<TModel, TProperty>> sortOptions) where TModel : class
        {
            Debug.Assert(sortOptions != null, "ApplySortOptions cannot accept a null sortOptions input.");
            Debug.Assert(sortOptions.Count() > 0, "At least one sort order must be specified to ApplySortOptions' sortOptions input.");
    
            var firstSortOption = sortOptions.OrderBy(o => o.Priority).First();
            var propertyName = firstSortOption.Property.MemberWithoutInstance();
            var isAscending = firstSortOption.IsAscending;
    
            // Perform the first sort action
            var results = isAscending ? collection.OrderBy(propertyName) : collection.OrderByDescending(propertyName);
    
            // Loop through all of the rest ISortOptions
            foreach (var sortOption in sortOptions.OrderBy(o => o.Priority).Skip(1))
            {
                // Make a copy of this or our deferred execution will bite us later.
                var currentOption = sortOption;
    
                propertyName = currentOption.Property.MemberWithoutInstance();
                isAscending = currentOption.IsAscending;
    
                // Perform the additional orderings.
                results = isAscending ? results.ThenBy(propertyName) : results.ThenByDescending(propertyName);
            }
    
            return results;
        }
    

    using the code from this question‘s answer:

        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
        {
            return ApplyOrder(source, property, "OrderBy");
        }
    
        public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
        {
            return ApplyOrder(source, property, "OrderByDescending");
        }
    
        public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
        {
            return ApplyOrder(source, property, "ThenBy");
        }
    
        public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
        {
            return ApplyOrder(source, property, "ThenByDescending");
        }
    
        private static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
        {
            var props = property.Split('.');
            var type = typeof (T);
            var arg = Expression.Parameter(type, "x");
            Expression expr = arg;
            foreach (var prop in props)
            {
                // use reflection (not ComponentModel) to mirror LINQ
                var pi = type.GetProperty(prop);
                expr = Expression.Property(expr, pi);
                type = pi.PropertyType;
            }
            var delegateType = typeof (Func<,>).MakeGenericType(typeof (T), type);
            var lambda = Expression.Lambda(delegateType, expr, arg);
    
            var 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;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.