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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:47:50+00:00 2026-06-10T13:47:50+00:00

Can someone explain the syntax for building an Expression that will OrderBy a user-specified

  • 0

Can someone explain the syntax for building an Expression that will OrderBy a user-specified property on an entity?

This MSDN article goes a long way to helping, but it deals with a simple list of strings, my data set contains my own custom objects.

http://msdn.microsoft.com/en-us/library/bb882637.aspx

  • 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-10T13:47:52+00:00Added an answer on June 10, 2026 at 1:47 pm

    Code first, explanation later.

    IQueryable<T> data = this.Database.ObjectsOfType<T>();
    
    var eachItem = Expression.Parameter(typeof(T), "item");
    var propertyToOrderByExpression = Expression.Property(eachItem, propertyName);
    
    var runMe = Expression.Call(
        typeof(Queryable),
        "OrderBy",
        new Type[] { data.ElementType, typeof(IComparable) },
        data.Expression,
        Expression.Lambda<Func<T,IComparable>>(propertyToOrderByExpression, new ParameterExpression[] { eachItem }));
    

    So, first we get hold of the data as a Queryable object. This has a kind of ‘root’ Expression property, and we need that.

    The eachItem thing is an expression that represents the argument placeholder in a Lambda, the symbol in the goes to, if you will.

    Then we make an expression that does the read operation the property name specified by the user in propertyName.

    We finally build an expression which does the call to the OrderBy method on the Queryable data. We’re saying (in order of argument):

    Expression.Call(
     [what's the type on which we want to call a method?],
     [what's the name of the method we're calling?],
     [if this method is generic, what are the types it deals with?],
     {
      [expression representing the data],
      [expression for the lambda using the reader exp + each item exp]
     })
    

    The last two are in { } since its actually a param array. I used IComparable since the property could be any type but obviously needs to be comparable to be ordered.

    Luke

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

Sidebar

Related Questions

Can someone explain in plain english the syntax of this: Here is the signature
Can someone explain to me what syntax this SQL query matches? SELECT DISTINCT boats.boatid
Can someone explain this method declaration syntax for me? In this function, the number
Can someone explain the syntax in this block of code? Invoke((MethodInvoker) ( () =>
Can someone explain to me why this command reports The syntax of the command
Can someone explain me the syntax below line 1? I am OK with js
Can someone explain this simple, yet deceiving, anomaly? There are two models, where B
Can someone explain me why this works: select val1, val2, count(case when table2.someID in
Can someone explain why, in this situation, scandir is getting my directory, but the
Can someone explain step by step how this query is processed? SELECT F.ID, F.FirstName,

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.