Home > Backend Development > C++ > How to Compose Expressions to Transform 'obj => obj.Prop' into 'parent => parent.obj.Prop'?

How to Compose Expressions to Transform 'obj => obj.Prop' into 'parent => parent.obj.Prop'?

Susan Sarandon
Release: 2025-01-18 07:02:17
Original
454 people have browsed it

如何组合表达式将 "obj => obj.Prop" 转换为 "parent => parent.obj.Prop"

Combine expressions to convert "obj => obj.Prop" to "parent => parent.obj.Prop"

The existing expression "obj => obj.Prop" can be converted into a new expression "parent => by combining the original expression with an expression that extracts "obj" from "parent" parent.obj.Prop".

Custom expression combination function

To create combinations, functions can be combined using custom expressions:

public static Expression<Func<T, TResult>> Compose<T, TIntermediate, TResult>(
    this Expression<Func<T, TIntermediate>> first,
    Expression<Func<TIntermediate, TResult>> second)
{
    return Expression.Lambda<Func<T, TResult>>(
        second.Body.Replace(second.Parameters[0], first.Body),
        first.Parameters[0]);
}
Copy after login

This function combines two expressions by replacing references to the parameters of the second expression with the body of the first expression.

Expression replacement

To replace an expression, you can use the custom expression accessor:

public class ReplaceVisitor : ExpressionVisitor
{
    private readonly Expression from, to;
    public ReplaceVisitor(Expression from, Expression to)
    {
        this.from = from;
        this.to = to;
    }

    public override Expression Visit(Expression ex)
    {
        if (ex == from) return to;
        else return base.Visit(ex);
    }
}

public static Expression Replace(this Expression ex,
    Expression from,
    Expression to)
{
    return new ReplaceVisitor(from, to).Visit(ex);
}
Copy after login

This accessor replaces all occurrences of one expression with another expression.

Combined expression

Using the functions above, it is possible to combine the original expression and the "obj" extracted expression:

Expression<Func<Customer, object>> propertySelector = cust => cust.Name;
Expression<Func<CustomerModel, Customer>> modelSelector = model => model.Customer;
Expression<Func<CustomerModel, object>> magic = modelSelector.Compose(propertySelector);
Copy after login

The generated expression "magic" will now select the "Name" property from the "Customer" in the "CustomerModel".

This revised response maintains the original content's structure and language while rephrasing sentences and using synonyms to achieve paraphrasing. The image remains in the same position and format.

The above is the detailed content of How to Compose Expressions to Transform 'obj => obj.Prop' into 'parent => parent.obj.Prop'?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template