如何在运行时为LINQ和Entity Framework生成链式方法表达式?

qua*_*els 3 c# lambda expression

我正在使用一个小型库,在运行时生成"where"表达式.我已经能够使用不同的运算符导航对象属性和查询Expression.Equal,Expression.NotEqual甚至是.Contains()字符串上的方法.

我遇到了一种情况,我需要创建一个表示链式方法的表达式,如下所示:x => x.SomeColumn.Trim().EndsWith("SomeText").我不知道从哪里开始.

.EndsWith()已经像这样实现了这个方法:

static Expression<Func<TEntity, bool>> GetEndsWithExpression(
    ParameterExpression parameterExpression,
    Expression propertyExpression,
    Expression valueToFind)
{
    var propertyExp = propertyExpression;
    var method = typeof(string).GetMethod("EndsWith", new[] { typeof(string) });
    var someValue = valueToFind;
    var containsMethodExp = Expression.Call(propertyExp, method, someValue);

    return Expression.Lambda<Func<TEntity, bool>>(containsMethodExp, parameterExpression);
}
Run Code Online (Sandbox Code Playgroud)

我想知道你是否可以帮我弄清楚如何添加.Trim()方法并将其与.EndsWith()方法链接起来.

其他一些信息,我已经在我的项目中使用了LINQKit,所以.AsExpandable()对我来说有些熟悉.

我的初始(错误)方法(更新)

我认为解决方案看起来像这样:

static Expression<Func<TEntity, bool>> GetTrimEndsWithExpression(
    ParameterExpression parameterExpression,
    Expression propertyExpression,
    Expression valueToFind)
{
    var propertyExp = propertyExpression;

    var trimMethod = typeof(string).GetMethod("Trim");
    var endsWithMethod = typeof(string).GetMethod("EndsWith", new[] { typeof(string) });

    var trimMethodExpression = Expression.Call(propertyExp, trimMethod).Expand();
    var containsMethodExp = Expression.Call(trimMethodExpression, endsWithMethod, valueToFind);

    return Expression.Lambda<Func<TEntity, bool>>(containsMethodExp, parameterExpression);
}
Run Code Online (Sandbox Code Playgroud)

但是,这无法编译.它抛出一个错误:

System.Reflection.AmbiguousMatchException: Ambiguous match found.
Run Code Online (Sandbox Code Playgroud)

如何在运行时生成的表达式中链接这两个方法?

Ser*_*rvy 5

处理这个问题的简单方法是编写一个Compose方法,允许您在另一个表达式中组合一个表达式,这在一般情况下解决了这个问题:

public static Expression<Func<T, TResult>> Compose<T, TIntermediate, TResult>(
    this Expression<Func<T, TIntermediate>> first,
    Expression<Func<TIntermedaite, TResult>> second)
{
    return Expression.Lambda<Func<T, TResult>>(
        second.Body.Replace(second.Parameters[0], first.Body),
        first.Parameters[0]);
}
Run Code Online (Sandbox Code Playgroud)

它使用该Replace方法将一个表达式的所有实例替换为另一个表达式,如下所示:

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

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

public static Expression Replace(this Expression ex,
    Expression from,
    Expression to)
{
    return new ReplaceVisitor(from, to).Visit(ex);
}
Run Code Online (Sandbox Code Playgroud)

现在我们有了这个,我们可以组合我们想要的表达式:

public static Expression<Func<TEntity, bool>> EndsWith<TEntity>(
    public Expression<Func<TEntity, string>> propertySelector,
    string endsWith)
{
    return propertySelector.Compose(str => str.Trim().EndsWith(endsWith));
}
Run Code Online (Sandbox Code Playgroud)