修改表达式<func <t,bool =“”>&gt;

时间:2016-11-18 07:21:50

标签: c# .net

我还没有使用过表达式,所以我无法说出我的意图是否有任何意义。我环顾了各种各样的网络,但没有用。

说我有这样的方法

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));
}

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

我的实际问题是如何修改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);

1 个答案:

答案 0 :(得分:4)

您可以修改方法内部的表达式,但它有点复杂,您需要根据具体情况进行修改。

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

的修改

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:四