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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:04:14+00:00 2026-05-28T06:04:14+00:00

I would like to create a method that orders an IEnumerable List by a

  • 0

I would like to create a method that orders an IEnumerable List by a given property where the property is passed into the method by a string i.e. (Mind you the first code example does not work, but the second does and is what I am trying to emulate dynamically).

string sortName = "SerialNumber";
IEnumerable<PartSummary> partList = FunctionToCreateList();
partOrderedList = partList.OrderBy(what do I stick in here);

that would be equivalent to

IEnumerable<PartSummary> partList = FunctionToCreateList();
partOrderedList = partList.OrderBy(p => p.SerialNumber);

How can I accomplish 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-28T06:04:15+00:00Added an answer on May 28, 2026 at 6:04 am

    Are you saying you want to pass the order by in to your method? If so, you can use this:

    Expression<Func<PartSummary, bool>> orderByClause
    

    Then you can do this:

    partOrderedList = partList.OrderBy(orderByClause);
    

    Then you can handle your order by in your business layer or wherever you wish.

    Okay, update: If you want to pass in the column name as a string you can do something like as follows:

    Create a static class for an extension method (reference: http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/39028ad2-452e-409f-bc9e-d1b263e921f6/):

    static class LinqExtensions
    {
        public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string sortingColumn, bool isAscending)
        {
    
            if (String.IsNullOrEmpty(sortingColumn))
            {
                return source;
            }
    
            ParameterExpression parameter = Expression.Parameter(source.ElementType, String.Empty);
    
            MemberExpression property = Expression.Property(parameter, sortingColumn);
            LambdaExpression lambda = Expression.Lambda(property, parameter);
    
            string methodName = isAscending ? "OrderBy" : "OrderByDescending";
    
            Expression methodCallExpression = Expression.Call(typeof(Queryable), methodName,
                                                new Type[] { source.ElementType, property.Type },
                                                source.Expression, Expression.Quote(lambda));
    
            return source.Provider.CreateQuery<T>(methodCallExpression);
        }
    }
    

    Then you can create your method:

       static IQueryable<PartSummary> FunctionToCreateList()
        {
            IList<PartSummary> list = new List<PartSummary>();
            list.Add(new PartSummary
                         {
                             Id = 1,
                             SerialNumber = "A",
                         });
            list.Add(new PartSummary
                         {
                             Id = 2,
                             SerialNumber = "B",
                         });
            return list.AsQueryable();
        }
    

    And then call your method:

       static void Main(string[] args)
        {
            IQueryable<PartSummary> partOrderedList = FunctionToCreateList();
            PartSummary partSummary = new PartSummary();
            string sortBy = "Id";
    
            partOrderedList = partOrderedList.OrderBy(sortBy, false);
    
            foreach (PartSummary summary in partOrderedList)
            {
                Console.WriteLine(summary.Id + ", " + summary.SerialNumber);
            }
            Console.ReadLine();
    
        }
    

    Now you can pass in the column name as a string and sort.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a method that takes an event as an argument
I would like to create a safe sum extension method that would have the
I would like to create a String.replaceAll() method in JavaScript and think using a
I would like to create a method in a base generic class to return
I would like create my own collection that has all the attributes of python
I would like create a web service in ASP.Net 2.0 that will supports JSON.
I would like to create a class that describes a file resource and then
I would like to create a simple drop down list with static values which
I would like to create a class whose methods can be called from multiple
Would like to create a strong password in C++. Any suggestions? I assume it

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.