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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:47:32+00:00 2026-05-17T01:47:32+00:00

I’ve been following with great interest the converstaion here: Construct Query with Linq rather

  • 0

I’ve been following with great interest the converstaion here:

Construct Query with Linq rather than SQL strings

with regards to constructing expression trees where even the table name is dynamic.

Toward that end, I’ve created a Extension method, addWhere, that looks like:

static public IQueryable<TResult> addWhere<TResult>(this IQueryable<TResult> query, string columnName, string value)
{
    var providerType = query.Provider.GetType();
    // Find the specific type parameter (the T in IQueryable<T>)
    var iqueryableT = providerType.FindInterfaces((ty, obj) => ty.IsGenericType && ty.GetGenericTypeDefinition() == typeof(IQueryable<>), null).FirstOrDefault();
    var tableType = iqueryableT.GetGenericArguments()[0];
    var tableName = tableType.Name;
    var tableParam = Expression.Parameter(tableType, tableName);
    var columnExpression = Expression.Equal(
        Expression.Property(tableParam, columnName),
        Expression.Constant(value));
    var predicate = Expression.Lambda(columnExpression, tableParam);
    var function = (Func<TResult, Boolean>)predicate.Compile();
    var whereRes = query.Where(function);
    var newquery = whereRes.AsQueryable();
    return newquery;
}

[thanks to Timwi for the basis of that code]

Which functionally, works.

I can call:

query = query.addWhere("CurUnitType", "ML 15521.1");

and it’s functionally equivalent to :

query = query.Where(l => l.CurUnitType.Equals("ML 15521.1"));

ie, the rows returned are the same.

However, I started watching the sql log, and I noticed with the line:

query = query.Where(l => l.CurUnitType.Equals("ML 15521.1"));

The Query generated is:

SELECT (A bunch of columns)
FROM [dbo].[ObjCurLocView] AS [t0]
WHERE [t0].[CurUnitType] = @p0

whereas when I use the line

query = query.addWhere("CurUnitType", "ML 15521.1");

The query generated is :

SELECT (the same bunch of columns)
FROM [dbo].[ObjCurLocView] AS [t0]

So, the comparison is now happening on the client side, instead of being added to the sql.

Obviously, this isn’t so hot.

To be honest, I mostly cut-and-pasted the addWhere code from Timwi’s (slightly different) example, so some of it is over my head. I’m wondering if there’s any adjustment I can make to this code, so the expression is converted into the SQL statement, instead of being determined client-side

Thanks for taking the time to read through this, I welcome any comments, solutions, links, etc, that could help me with this. And of course if I find the solution through other means, I’ll post the answer here.

Cheers.

  • 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-17T01:47:32+00:00Added an answer on May 17, 2026 at 1:47 am

    The big problem is that you’re converting the expression tree into a delegate. Look at the signature of Queryable.Where – it’s expressed in expression trees, not delegates. So you’re actually calling Enumerable.Where instead. That’s why you need to call AsQueryable afterwards – but that doesn’t do enough magic here. It doesn’t really put it back into “just expression trees internally” land, because you’ve still got the delegate in there. It’s now wrapped in an expression tree, but you’ve lost the details of what’s going on inside.

    I suspect what you want is this:

    var predicate = Expression.Lambda<Func<TResult, Boolean>>
          (columnExpression, tableParam);
    return query.Where(predicate);
    

    I readily admit that I haven’t read the rest of your code, so there may be other things going on… but that’s the core bit. You want a strongly typed expression tree (hence the call to the generic form of Expression.Lambda) which you can then pass into Queryable.Where. Give it a shot 🙂

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

Sidebar

Related Questions

No related questions found

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.