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

  • Home
  • SEARCH
  • 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 5930033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:30:07+00:00 2026-05-22T14:30:07+00:00

On a project built with .NET 3.5, I am using LINQ expressions to dynamically

  • 0

On a project built with .NET 3.5, I am using LINQ expressions to dynamically generate code at runtime. The LINQ expressions are compiled using the Compile method and stored for later use as predicates with LINQ to objects.

The expressions are sometimes quite complicated and difficult to debug.

Below is an example of an expression viewed through the debugger visualizer in Visual Studio.

{request
=> (Invoke(workEnvelopeHead
=> (workEnvelopeHead.Method = value(Wombl.Scenarios.CannedResponses+<>c_DisplayClass58).pipeline),
request.WorkEnvelope.Head)
And Invoke(body =>
Invoke(value(Wombl.Scenarios.CannedResponses+<>c
_DisplayClass78).isMatch,
body.SingleOrDefault()),Convert(request.WorkEnvelope.Body.Any)))}

I would like to be able to optimize expressions like the above so that the value(Wombl.Scenarios.CannedResponses+<>c__DisplayClass58).pipeline expression is replaced with a constant that is the variable’s value.

In this particular case, value(Wombl.Scenarios.CannedResponses+<>c__DisplayClass58).pipeline is a reference in the lambda to a variable in the parent scope. Something like:

var pipeline = "[My variable's value here]";
// My lambda expression here, which references pipeline
// Func<RequestType, bool> predicate = request => ........ workEnvelopeHead.Method == pipeline ..........

The original expression, optimized ought to look like:

{request => (Invoke(workEnvelopeHead =>
(workEnvelopeHead.Method = “[My variable’s value here]”,
request.WorkEnvelope.Head) And Invoke(body => > Invoke(value(Wombl.Scenarios.CannedResponses+<>c__DisplayClass78).isMatch,
body.SingleOrDefault()),Convert(request.WorkEnvelope.Body.Any)))}

How can I make such optimizations at runtime to the LINQ expression, before compiling?

  • 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-22T14:30:08+00:00Added an answer on May 22, 2026 at 2:30 pm

    So I went ahead and wrote an expression visitor that replaces the variable references with the actual value. It wasn’t so hard to do after all.

    Usage:

    var simplifiedExpression = ExpressionOptimizer.Simplify(complexExpression);
    

    The class:

    It inherits from ExpressionVisitor which came from the code samples on this page because in .NET 3.0 it is internal. In .NET 4.0 the class is public but might require some changes to this class.

    public sealed class ExpressionOptimizer : ExpressionVisitor
    {
        private ExpressionOptimizer()
        {
        }
    
        #region Methods
    
        public static Expression<TDelegate> Simplify<TDelegate>(Expression<TDelegate> expression)
        {
            return expression == null
                       ? null
                       : (Expression<TDelegate>) new ExpressionOptimizer().Visit(expression);
        }
    
        private static bool IsPrimitive(Type type)
        {
            return type.IsPrimitive
                   || type.IsEnum
                   || type == typeof (string)
                   || type == typeof (DateTime)
                   || type == typeof (TimeSpan)
                   || type == typeof (DateTimeOffset)
                   || type == typeof (Decimal)
                   || typeof(Delegate).IsAssignableFrom(type);
        }
    
        protected override Expression VisitMemberAccess(MemberExpression memberExpression)
        {
            var constantExpression = memberExpression.Expression as ConstantExpression;
    
            if (constantExpression == null || !IsPrimitive(memberExpression.Type))
                return base.VisitMemberAccess(memberExpression);
    
            // Replace the MemberExpression with a ConstantExpression
            var constantValue = constantExpression.Value;
            var propertyInfo = memberExpression.Member as PropertyInfo;
            var value = propertyInfo == null
                            ? ((FieldInfo) memberExpression.Member).GetValue(constantValue)
                            : propertyInfo.GetValue(constantValue, null);
    
            return Expression.Constant(value);
        }
    
        #endregion
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I'm working on Web project that is built using ASP.NET Web Forms. We
I'm using the mvc-mini-profiler in my project built with ASP.Net MVC 3 and Entity
I am using CruiseControl.NET to build a C# project. I am using an msbuild
I have a website project that I want to build using CruiseControl.net. On one
A group of us are working on a project which we built with .NET
I have a .NET project which references another assembly that is built outside of
The ASP.NET MVC 1.0 (final) project template has basic membership built in, but I
I have built a web setup project in VS2008 which installs my ASP.NET/Silverlight app
iv used the built in membership control for my ASP.net project, this create the
Project being built on ASP.net v 2 VB.net IIS 6 SQL Server 2005 database

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.