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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:20:20+00:00 2026-06-17T20:20:20+00:00

T is a type that may or may not have a specific property, lets

  • 0

T is a type that may or may not have a specific property, lets say ‘City’. For the types that have a property named ‘City’, I would like to restrict the records such that only residents of the Gotham are returned and they are sorted.

public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string ordering, params object[] values) 
{

    var type = typeof(T);
    var property = type.GetProperty(ordering);
    var parameter = Expression.Parameter(type, "p");
    var propertyAccess = Expression.MakeMemberAccess(parameter, property);
    var orderByExp = Expression.Lambda(propertyAccess, parameter);
    MethodCallExpression resultExp = Expression.Call(
                typeof(Queryable), 
                "OrderBy", 
                new     Type[] { type, property.PropertyType }, 
                source.Expression, 
                Expression.Quote(orderByExp));


    string propertyToRestrictOn = "City";
    string restrictedValue = "Gotham";
    var restrictedProperty = type.GetProperty(propertyToRestrictOn);
    if(null ! = restrictedProperty )
    {
      // TODO: What to add here so than only those records are returned that have a 
      // property named City and the value is 'Gotham'???
    }

   return source.Provider.CreateQuery<T>(resultExp);
}

if possible please name/link some helpful literature here as well just in case I have to create more complex queries i.e. mix And/OR

The code was borrowed from
How do I apply OrderBy on an IQueryable using a string column name within a generic extension method?

  • 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-17T20:20:21+00:00Added an answer on June 17, 2026 at 8:20 pm

    I think you’re making this harder than you have to. In the first part of your code (the OrderBy()), you don’t actually need to generate the whole query expression, just the lambda inside it.
    And in the second part (the optional Where()) you can do pretty much the same thing, just add Expression.Equal() and Expression.Constant():

    public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string ordering)
    {
        var type = typeof(T);
        var property = type.GetProperty(ordering);
        var parameter = Expression.Parameter(type, "p");
        var propertyAccess = Expression.MakeMemberAccess(parameter, property);
        // necessary for value types to work
        var cast = Expression.Convert(propertyAccess, typeof(object));
        var orderByExp = Expression.Lambda<Func<T, object>>(cast, parameter);
    
        IQueryable<T> result = source.OrderBy(orderByExp);
    
        string propertyToRestrictOn = "City";
        string restrictedValue = "Gotham";
        var restrictedProperty = type.GetProperty(propertyToRestrictOn);
        if (restrictedProperty != null)
        {
            var restrictionParameter = Expression.Parameter(type, "p");
            var restrictionPropertyAccess =
                Expression.MakeMemberAccess(restrictionParameter, restrictedProperty);
            var restrictionEquality =
                Expression.Equal(restrictionPropertyAccess,
                                 Expression.Constant(restrictedValue));
            var whereExp =
                Expression.Lambda<Func<T, bool>>(restrictionEquality, restrictionParameter);
    
            result = result.Where(whereExp);
        }
    
       return result;
    }
    

    Also, if your method is going to do more than just ordering, I think you shouldn’t call it OrderBy().

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

Sidebar

Related Questions

I have generic type that looks like: public class GenericClass<T, U> where T :
I would like to create a c++ type that mimic the build-in type exactly.
I have an archetype content type that previously was not folderish. Now I have
This may or may not be specific to VS2005 (as that is the version
I have an idea for a website that I would like to develop and
When defining parameter type that is e.g. in System.Data you have no intellisense and
I have a data type that contains a set and a method that expects
I currently have a type that I inject into my controllers that's used for
I have a site content type that was used for a handful of lists
Unit tests have different requirements than production code. For example, unit tests may not

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.