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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:34:12+00:00 2026-05-16T03:34:12+00:00

I have a query that needs to be reused all over the place and

  • 0

I have a query that needs to be reused all over the place and I need to vary which property/column gets used for a join.

What I’d like to be able to do is something like:

query = RestrictByProp(query, x=>x.ID);

An extremely simplified RestrictByProp() could be*:

private static IQueryable<Role> RestrictByProp(IQueryable<Role> query, 
                                               Func<Role, int> selector)
{
    return query.Where(x => selector(x) == 1);
}

The problem is that even this simple implementation causes a runtime exception:

Method 'System.Object DynamicInvoke(System.Object[])' has no 
supported translation to SQL.

**(Here I’m just adding a simple ‘where’ clause – in my real code I’d be using the lambda to pick which property to use for a join).*

I find this strange because if the member access lambda is done inline it is fine:

 private static IQueryable<Role> RestrictByID(IQueryable<Role> query)
 {
     return query.Where(x=> x.ID == 1);
 }

LINQ to SQL is also happy if you pass in an Expression<Func<Role, bool>> (i.e. when the parameter is x=>x.ID == 1) but that defeats the object because I need the value of the right-hand operand to be determined within the query.

Is there a way to somehow munge the lambda expression in RestrictByProp() so that LINQ to SQL knows how to generate the SQL?

  • 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-16T03:34:12+00:00Added an answer on May 16, 2026 at 3:34 am

    First, you need to change your method signature:

    private static IQueryable<Role> RestrictByProp(IQueryable<Role> query, 
        Expression<Func<Role, int>> selector)
    

    That will mean your lambda expression is converted into an expression tree instead of a delegate.

    You’ll then need to build an Expression<Func<Role, bool>> from the existing expression tree.

    It will look something like this:

    LambdaExpression lambda = (LambdaExpression) selector;
    var predicate = Expression.Equal(selector, Expression.Constant(1));
    var lambdaPredicate = Expression.Lambda<Func<Role, bool>>(predicate,
                                                              lambda.Parameters);
    return query.Where(lambdaPredicate);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query such that I need to get A specific dog All
Say I have a query that fetches [type][show_name]. For all [type]==5 records, I need
I have a linq query that needs to pull a date column out of
I have a query that needs to check that all fields have values are
I have below a working query that needs to be simplified. The reason is
I have a result of a db query in java.sql.ResultSet that needs to be
I have a query that I need to execute that I do not know
Using Microsoft SQL server 2008 I have a query that is in need of
I have a dynamic query string that I need to pass via an .htaccess
I have a query that does what i want joining table but i need

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.