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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:37:45+00:00 2026-05-25T12:37:45+00:00

The .NET Expression type has support for representing calling into a function defined by

  • 0

The .NET Expression type has support for representing calling into a function defined by another expression, but is there a way of expressing this using the C# lambda expression syntax? I can generate one expression, compile it and call it in another expression but that loses the “expressionness” of the lambda being called, and the calling expression will only contain a reference to an anonymous compiled function.

I.e., what I want to do is something like:

Expression<Func<int, int>> f = x => x + x;
Expression<Func<int, int>> g = x => 2 + f(x);

but as it’s not valid to call an Expression directly, the closest thing I have is:

Expression<Func<int, int>> f = x => x + x;
var f_ = f.Compile();
Expression<Func<int, int>> g = x => 2 + f_(x);

… which loses the fact that the function f being called is an expression.

  • 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-25T12:37:45+00:00Added an answer on May 25, 2026 at 12:37 pm

    I don’t believe this is currently possible to do with the compiler’s help (as of C# 4.0), but you could of course do it “by hand” with Expression.Invoke.

    Creates an InvocationExpression that applies a delegate or lambda
    expression to a list of argument expressions.

    In your case, this would look something like:

    Expression<Func<int, int>> f = x => x + x;
    
    var parameter = Expression.Parameter(typeof(int),"y");
    var body = Expression.Add(Expression.Constant(2), Expression.Invoke(f, parameter));
    
    // Loosely speaking: y => 2 + f(y)
    var g = Expression.Lambda<Func<int, int>>(body, parameter);
    

    LINQKit provides the Invoke /Expand extension-methods that do the leg-work for you, giving you back the ability to use lambdas for this. This will let you do:

    Expression<Func<int, int>> f = x => x + x;
    Expression<Func<int, int>> g = x => 2 + f.Invoke(x);
    
    // Note: 'Inlines' the invocation into the target expression.
    var finalExpression = g.Expand();
    

    ..which is very close to the syntax you had originally wanted.

    One point to note about InvocationExpressions is that some LINQ providers (such as LINQ to Entities) don’t like them at all; so you’ll have often have to ‘expand’ out the invoked expression so that it looks like one single expression to the provider. You can of course do this by hand, but LINQKit again comes in handy with the AsExpandable extension.

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

Sidebar

Related Questions

What is the syntax to concatenate text into a binding expression for an asp.net
In .NET there seem to be two ways to pass a type to a
How to write cron expression (for quartz.net , but that shouldn't matter) that would
I'm looking for a .NET regular expression extract all the URLs from a webpage
What is the cast expression equivalent of VB.NET's CType in Visual Basic 6?
I'm working on a regular expression in a .NET project to get a specific
i am using asp.net 3.5 and VB.net, i need a validation expression to validate
Is there a built-in mechanism in .NET to match patterns other than regular expressions
I have a syntax highlighting function in vb.net. I use regular expressions to match
I have an ASP.NET site that has been running perfectly for a long time,

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.