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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:03:52+00:00 2026-05-23T17:03:52+00:00

If I have two nearly identical classes Animal and AnimalViewModel and an expression tree

  • 0

If I have two nearly identical classes Animal and AnimalViewModel and an expression tree related to the viewmodel, how can I translate it to Animal?

public class Animal
{
   public string Species { get; set; }
   public string Name { get; set; }
   public string Sound { get; set; }
}
public class AnimalViewModel : ViewModelBase
{
   public string Species { get; set; }
   public string Name { get; set; }
   public string Sound { get; set; }
}

How can I translate an Expression<Func<AnimalViewModel,bool>> to Expression<Func<Animal,bool>>?

public static Expression<Func<Animal,bool>> Translate (Expression<Func<AnimalViewModel,bool>> expression)
{
  // What goes here?  I assume I have to traverse the tree somehow.
}
  • 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-23T17:03:53+00:00Added an answer on May 23, 2026 at 5:03 pm

    Here’s a visitor that does the job.

    • it makes a copy of the parameter (since we’ll need to create a new parameter and substitute all references of the old parameter for the new one)
    • it walks the .Body of the tree, substituting the parameter, and switching any member-access against the old type to a like-named member on the new type
    • it re-assembles a lambda using the parameter we invented earler

    Code:

    class TypeChangeVisitor : ExpressionVisitor
    {
        private readonly Type from, to;
        private readonly Dictionary<Expression, Expression> substitutions;
        public TypeChangeVisitor(Type from, Type to, Dictionary<Expression, Expression> substitutions)
        {
            this.from = from;
            this.to = to;
            this.substitutions = substitutions;
        }
        public override Expression  Visit(Expression node)
        { // general substitutions (for example, parameter swaps)
            Expression found;
            if(substitutions != null && substitutions.TryGetValue(node, out found))
            {
                return found;
            }
            return base.Visit(node);
        }
        protected override Expression VisitMember(MemberExpression node)
        { // if we see x.Name on the old type, substitute for new type
            if (node.Member.DeclaringType == from)
            {
                return Expression.MakeMemberAccess(Visit(node.Expression),
                    to.GetMember(node.Member.Name, node.Member.MemberType,
                    BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).Single());
            }
            return base.VisitMember(node);
        }
    }
    public class Program
    {
        public static void Main()
        {
            Expression<Func<AnimalViewModel, bool>> predicate = x => x.Name == "abc";
            var switched = Translate<AnimalViewModel, Animal>(predicate);
        }
        public static Expression<Func<TTo, bool>> Translate<TFrom, TTo>(Expression<Func<TFrom, bool>> expression)
        {
            var param = Expression.Parameter(typeof(TTo), expression.Parameters[0].Name);
            var subst = new Dictionary<Expression, Expression> { { expression.Parameters[0], param } };
            var visitor = new TypeChangeVisitor(typeof(TFrom), typeof(TTo), subst);
            return Expression.Lambda<Func<TTo, bool>>(visitor.Visit(expression.Body), param);
        }
    }
    

    Note that if you have x.Something.Name you might need to be a bit more careful, but this should get you a reasonable way.

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

Sidebar

Related Questions

So, lets say I have two nearly identical classes in C# and Ruby: C#
I have two nearly identical functions, but I'm not sure how I can refactor
I have a couple classes that have nearly identical code. Only a string or
I have two profiles that are nearly identical. Rather than duplicating the configuration in
I have two nearly identical queries operating on essentially the same fields in two
I'm working on some legacy Django code. I have two nearly-identical views: @login_required def
I have two nearly identical javascript functions that are used to initiate a jquery
I have two click-events, that are nearly similar, but not quite. I am wondering
I've moved from SeleniumRC to WebDriver for nearly two years. But I have to
I have two classes generated by LINQ2SQL both from the same table so they

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.