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

  • Home
  • SEARCH
  • 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 7888499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:50:49+00:00 2026-06-03T05:50:49+00:00

Possible Duplicate: LINQ: orderby with dynamic string parameter i’m sorting a IEnumarable with an

  • 0

Possible Duplicate:
LINQ: orderby with dynamic string parameter

i’m sorting a IEnumarable with an OrderBy() clause. I’v a list with string with a value which contains the field I wanna order by. Now I’m using a switch statement for each property name.

  swich (propertyname)
    case "name":
      list = list.OrderBy(l=>l.name).ToList();
      break;
    case "property":
      list = list.OrderBy(l=>l.property).ToList();
      .....

is there a simple solution to use the string “propertyname” as an attribute in the orderby clause?

As I’ve done it I get an solution that is far from desirable. Not only is it more work to code each property, but if in the future any attribute is added this update wil be forgotten in the function i’m writing.

hope someone has got a better solution.

  • 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-03T05:50:50+00:00Added an answer on June 3, 2026 at 5:50 am
    var list = new List<MyClass>();
    
    // Gets an object to give us access to the property with the given name of type MyClass
    var propertyInfo = typeof(MyClass).GetProperty(propertyName);
    
    // Each item is ordered by the value of the property
    list = list.OrderBy(x => propertyInfo.GetValue(x, null)).ToList();
    

    Explaining predicate:

    var xPropVal = propertyInfo.GetValue(x, null);
    

    Using the property info object, you get the value of the object x, with null parameters.
    The parameters will be used for indexers, in this case, since this is a property info object.

    But since the properties in this case are all simple properties, the second parameter should be null or an empty object array.

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

Sidebar

Related Questions

Possible Duplicate: How do I use LINQ Contains(string[]) instead of Contains(string) Suppose I have
Possible Duplicates: Sorting a list using Lambda/Linq to objects C# List<> OrderBy Alphabetical Order
Possible Duplicate: Concat all strings inside a List<string> using LINQ I am using C#
Possible Duplicate: 'Contains()' workaround using Linq to Entities? I am using the System.Linq.Dynamic library
Possible Duplicate: 'Contains()' workaround using Linq to Entities? Using LINQ in C#, I have
Possible Duplicate: “NOT IN” clause in LINQ to Entities I work with the schema
Possible Duplicate: LINQ list to sentence format (insert commas & “and”) Imagine these inputs
Possible Duplicate: LINQ to append to a StringBuilder from a String[] Forgive my functional
possible duplicate: Cannot serialize parameter of type ‘System.Linq.Enumerable… ’ when using WCF, LINQ, JSON
Possible Duplicate: Find sequence in IEnumerable<T> using Linq There is a string method: int

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.