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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:48:40+00:00 2026-05-15T09:48:40+00:00

It is fairly common to take an Expression tree, and convert it to some

  • 0

It is fairly common to take an Expression tree, and convert it to some other form, such as a string representation (for example this question and this question, and I suspect Linq2Sql does something similar).

In many cases, perhaps even most cases, the Expression tree conversion will always be the same, i.e. if I have a function

public string GenerateSomeSql(Expression<Func<TResult, TProperty>> expression)

then any call with the same argument will always return the same result for example:

GenerateSomeSql(x => x.Age)  //suppose this will always return "select Age from Person"
GenerateSomeSql(x => x.Ssn)  //suppose this will always return "select Ssn from Person"

So, in essence, the function call with a particular argument is really just a constant, except time is wasted at runtime re-computing it continuously.

Assuming, for the sake of argument, that the conversion was sufficiently complex to cause a noticeable performance hit, is there any way to pre-compile the function call into an actual constant?

Edit
It appears that there is no way to do this exactly within C# itself. The closest you can probably come within c# is the accepted answer (though of course you would want to make sure that the caching itself wasn’t slower than regenerating). To actually convert to true constants, I suspect that with some work, you could use something like mono-cecil to modify the bytecodes after compilation.

  • 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-15T09:48:40+00:00Added an answer on May 15, 2026 at 9:48 am

    The excellent LINQ IQueryable Toolkit project has a query cache that does something similar to what you’ve described. It contains an ExpressionComparer class that walks the hierarchy of two expressions and determines if they are equivalent. This technique is also used to collect references to common properties for parameterization and in the removal of redundant joins.

    All you would need to do is come up with an expression hashing strategy so you can store the results of your processed expressions in a dictionary, ready for future reuse.

    Your method would then look something like this:

    private readonly IDictionary<Expression, string> _cache
        = new Dictionary<Expression, string>(new ExpressionEqualityComparer());
    
    public string GenerateSomeSql(Expression<Func<TResult, TProperty>> expression)
    {
        string sql;
        if (!_cache.TryGetValue(expression, out sql))
        {
            //process expression
            _cache.Add(expression, sql);
        }
        return sql;
    }
    
    class ExpressionEqualityComparer : IEqualityComparer<Expression>
    {
        public bool Equals(Expression x, Expression y)
        {
            return ExpressionComparer.AreEqual(x, y);
        }
    
        public int GetHashCode(Expression obj)
        {
            return ExpressionHasher.GetHash(obj);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Fairly common question this, to which I have a few answers and I'm nearly
I have a situation that I'm sure must be fairly common. I have some
I have what appears to be a fairly common scenario: I have a database
This is a fairly trivial matter, but I'm curious to hear people's opinions on
I currently have a C# project which uses plugins and has a fairly common
This question on getting random values from a finite set got me thinking... It's
I have a fairly infrequent problem occuring with source control. In the example here
I think its fairly common practice these days not to include a mailto: tag
A fairly common requirement in database applications is to track changes to one or
I have a GridView and, using a fairly common method, I'm using a FooterRow

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.