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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:55:45+00:00 2026-05-24T13:55:45+00:00

How can I make a generic helper method to convert the type used by

  • 0

How can I make a generic helper method to convert the type used by a Func from one type to another within an Expression

I have a Expression<Func<IEmployee, bool>> and I want to convert it to a

Expression<Func<Employee, bool>>.

The second Type always implements the first Type. A generic solution is what I am trying to achieve.

Edit

I have edited the question to be clearer.

  • 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-24T13:55:45+00:00Added an answer on May 24, 2026 at 1:55 pm

    Well, you could create an expression that casts and then forwards its argument to the original expression:

    Expression<Func<IEmployee, bool>> source = ...
    
    var param = Expression.Parameter(typeof(Employee));
    
    // Types the argument as the type expected by the source expression
    // and then forwards it...
    var invocationExpr = Expression.Invoke
                         (source, Expression.TypeAs(param, typeof(IEmployee))); 
    
    var result = Expression.Lambda<Func<Employee, bool>>(invocationExpr, param);
    

    If the provider doesn’t support invocation expressions, you will probably need a much more
    sophisticated solution that replaces Parameters in the source expression.

    EDIT: Ok, since you say your provider doesn’t like the resulting expression, here’s an example of the alternative. It’s a really rough cut of what a parameter-replacer should look like (I just wrote this now as a sample), but it should work fine for your purposes.

    public static class ParameterReplacer
    {
        // Produces an expression identical to 'expression'
        // except with 'source' parameter replaced with 'target' parameter.     
        public static Expression<TOutput> Replace<TInput, TOutput>
                     (Expression<TInput> expression,
                      ParameterExpression source,
                      ParameterExpression target)
        {
            return new ParameterReplacerVisitor<TOutput>(source, target)
                      .VisitAndConvert(expression);
        }
    
        private class ParameterReplacerVisitor<TOutput> : ExpressionVisitor
        {
            private ParameterExpression _source;
            private ParameterExpression _target;
    
            public ParameterReplacerVisitor
                  (ParameterExpression source, ParameterExpression target)
            {
                _source = source;
                _target = target;
            }
    
            internal Expression<TOutput> VisitAndConvert<T>(Expression<T> root)
            {
                return (Expression<TOutput>)VisitLambda(root);
            }
    
            protected override Expression VisitLambda<T>(Expression<T> node)
            {
                // Leave all parameters alone except the one we want to replace.
                var parameters = node.Parameters.Select
                                 (p => p == _source ? _target : p);
    
                return Expression.Lambda<TOutput>(Visit(node.Body), parameters);
            }
    
            protected override Expression VisitParameter(ParameterExpression node)
            {
                // Replace the source with the target, visit other params as usual.
                return node == _source ? _target : base.VisitParameter(node);
            }
        }
    }
    

    And then use it as:

    Expression<Func<IEmployee, bool>> expression = ...
    
    var result = ParameterReplacer.Replace
                    <Func<IEmployee, bool>, Func<Employee, bool>>
                    (expression,
                     expression.Parameters.Single(), 
                     Expression.Parameter(typeof(Employee));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have: void add_all_msgs(std::deque<Message>::iterator &iter); How can I make that function generic, so it
Is there a way to make this method generic so I can return a
How can make a link within a facebox window that redirects it to another
I want to create a helper method that can take in ANY type of
Say, we have a generic class with a private List. We can make it
In java, one can make use of the generic LinkedList to improve the efficiency
I tried to make a generic helper method using Moq with nunit as described
I can make a variable's name from two variables' value: $a = 'tea'; $b
Can someone tell me how I can make the following output? I have a
How can I make this generic? class AtomicReference { private Object _value; public AtomicReference()

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.