修改表达式<Func<T, bool>>

Modifying Expression<Func<T, bool>>

我对表达式的处理不多,所以我不能说我的意图是否有意义。我浏览了互联网,但无济于事。

假设我有这样的方法

public async Task<T> GetFirstWhereAsync(Expression<Func<T, bool>> expression)
{
    // this would be a call to 3rd party dependency
    return await SomeDataProvider
        .Connection
        .Table<T>()
        .Where(expression)
        .FirstOrDefaultAsync();
}

从我的其他代码中我可以调用它,例如

private async Task<User> GetUser(Credentials credentials)
{
    return await SomeDataSource
        .GetFirstWhereAsync(u => u.UserName.Equals(credentials.UserName));
}

所以我会首先从我的 SomeDataProvider 收到与给定表达式匹配的 User


我的实际问题是我将如何着手修改 GetFirstWhereAsync 以便它将一些 SecretSauce 应用于传递给它的任何表达式?我可以在调用者中执行此操作,但那样会很丑陋而且没有多大乐趣。

所以如果我传入像

这样的表达式
u => u.UserName.Equals(credentials.UserName);
p => p.productId == 1;

我希望将这些修改为

u => u.UserName.Equals(SecretSauce.Apply(credentials.UserName));
p => p.productId == SecrectSauce.Apply(1);

你可以修改方法里面的表达式,但是有点复杂,需要具体情况具体分析。

下面的示例将处理从 x => x.Id == 1x => x.Id == SecretSauce.Apply(1)

的修改

Class

class User
{
    public int Id { get; set; }
    public string Name { get; set;}

    public override string ToString()
    {
        return $"{Id}: {Name}"; 
    }
}

class SquareSauce
{
    public static int Apply(int input)
    {
        // square the number
        return input * input;
    }
}

数据

User[] user = new[]
{
    new User{Id = 1, Name = "One"},
    new User{Id = 4, Name = "Four"},
    new User{Id = 9, Name = "Nine"}
};

方法

User GetFirstWhere(Expression<Func<User, bool>> predicate)
{
    //get expression body
    var body = predicate.Body;

    //get if body is logical binary (a == b)
    if (body.NodeType == ExpressionType.Equal)
    {
        var b2 = ((BinaryExpression)body);
        var rightOp = b2.Right;

        // Sauce you want to apply
        var methInfo = typeof(SquareSauce).GetMethod("Apply");      

        // Apply sauce to the right operand
        var sauceExpr = Expression.Call(methInfo, rightOp);

        // reconstruct equals expression with right operand replaced 
        // with "sauced" one
        body = Expression.Equal(b2.Left, sauceExpr);

        // reconstruct lambda expression with new body
        predicate = Expression.Lambda<Func<User, bool>>(body, predicate.Parameters);
    }
    /*
        deals with more expression type here using else if
    */
    else
    {
        throw new ArgumentException("predicate invalid");
    }

    return user
        .AsQueryable()
        .Where(predicate)
        .FirstOrDefault();
}

用法

Console.WriteLine(GetFirstWhere(x => x.Id == 2).ToString());

该方法会将 x => x.Id == 2 转换为 x => x.Id == SquareSauce.Apply(2) 并将生成:

4: Four