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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:59:41+00:00 2026-05-24T07:59:41+00:00

I have a method that I want to use to sort a list: private

  • 0

I have a method that I want to use to sort a list:

private static IQueryable<T> BuildQuery<T>(IQueryable<T> query, 
                                           string methodName, 
                                           Expression<Func<T, object>> property)             
    {
        var typeArgs = new[] { query.ElementType, property.Body.Type };

        methodCall = Expression.Call(typeof (Queryable),
                                                  methodName,
                                                  typeArgs,
                                                  query.Expression,
                                                  property);

        return query.Provider.CreateQuery<T>(methodCall);
    }

I get an exception when I execute the code using the following args:

var myPreExistingQuery = new List<SomeType>{ new SomeType() }.AsQueryable();
var query = BuildQuery(myPreExistingQuery, "OrderBy", x => x.SomeProperty);

The exception is:

No method 'OrderBy' on type 'System.Linq.Queryable' is compatible with the supplied arguments.

Can anyone see what I’m missing here?

EDIT:

I tried another overload of Expression.Call() and got the same same exception:

private static IQueryable<T> BuildQuery<T>(IQueryable<T> query, string methodName, Expression<Func<T, object>> propertyExpression)             
    {
        var methodCall = Expression.Call(query.Expression,
                                         methodName,
                                         new[] {query.ElementType, property.Body.Type},
                                         new[] {propertyExpression});

        return query.Provider.CreateQuery<T>(methodCall);
    }
  • 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-24T07:59:41+00:00Added an answer on May 24, 2026 at 7:59 am

    Since you want your property selector expression to make the appropriate calls dynamically, you must create a new expression for it. You cannot use the provided selector as-is since it is currently typed Expression<Func<T, object>> and not returning your specific type Expression<Func<T, SomeType>>. You might be able to get it to compile by changing the type arguments of the call to accept object but it will not work as expected since it will be doing object reference comparisons (and your LINQ provider will may reject it anyway).

    To recreate your selector expression, you could do this:

    private static IQueryable<T> BuildQuery<T>(
        IQueryable<T> query,
        string methodName,
        Expression<Func<T, object>> property)
    {
        var typeArgs = new[] { query.ElementType, property.Body.Type };
        var delegateType = typeof(Func<,>).MakeGenericType(typeArgs);
        var typedProperty = Expression.Lambda(delegateType, property.Body, property.Parameters);
    
        var methodCall = Expression.Call(
            typeof(Queryable),
            methodName,
            typeArgs,
            query.Expression,
            typedProperty);
    
        return query.Provider.CreateQuery<T>(methodCall);
    }
    

    A nice alternative to doing this would be to make the property type generic too. That way, you’ll get an appropriately strongly typed selector from the start.

    private static IQueryable<TSource> BuildQuery<TSource, TProperty>(
        IQueryable<TSource> query,
        string methodName,
        Expression<Func<TSource, TProperty>> property)
    {
        var typeArguments = property.Type.GetGenericArguments();
    
        var methodCall = Expression.Call(
            typeof(Queryable),
            methodName,
            typeArguments,
            query.Expression,
            property);
    
        return query.Provider.CreateQuery<TSource>(methodCall);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that returns a list of type string. I want to
I have a method that where I want to redirect the user back to
I have a method: myMethod() {} that I want to make accessible to javascript.
I have a method CreateAccount(...) that I want to unit test. Basically it creates
Say I want to have a method that takes any kind of number, is
I have a method that's about ten lines of code. I want to create
I have a method running on the EDT and within that I want to
I have a method in a class in another file that I want to
What would I do if I want to have a generic method that only
Basically, if I have a method (or several methods) that I want to expose

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.