C#表达式 - 从另一个表达式创建表达式

时间:2012-11-26 18:12:40

标签: c# linq-expressions

我正在尝试使用看起来像这样的表达式来创建一个可重用的方法:

Expression<Func<Order, bool>> CreateExpression(Expression<Func<Order, int>> parameter, FilterOperator operator, int value)

所以我可以这样使用它:

IQueryable<Order> orders = db.Orders;

var filtered = orders.Where(CreateExpression(o => o.OrderID, FilterOperator.GreaterThan, 100));

我不知道怎么写这个方法。如何编写一个为我创建此Expression的方法?

我需要能够做到这样的事情:

if(operator == FilterOperator.GreaterThan)
   return m => m.OrderID > value;
else if(operator == FilterOperator.LessThan)
   return m => m.OrderID < value;

但我想使用传入的表达式而不是直接使用OrderID。我怎么能这样做?

1 个答案:

答案 0 :(得分:5)

static Expression<Func<T, bool>> CreateExpression<T>(Expression<Func<T, int>> parameter, FilterOperator @operator, int value)
{
    var argExpr = Expression.Parameter(typeof(T), "p");
    var paramExpr = Expression.Invoke(parameter, argExpr);
    var constExpr = Expression.Constant(value);
    Expression compExpr = null;
    switch(@operator)
    {
        case FilterOperator.GreaterThan:
            compExpr = Expression.GreaterThan(paramExpr, constExpr);
            break;
        case FilterOperator.LessThan:
            compExpr = Expression.LessThan(paramExpr, constExpr);
            break;
    }

    return Expression.Lambda<Func<T, bool>>(compExpr, argExpr);
}

如果您无法使用Invoke并且您的parameter表达式是成员表达式,那么您可以使用新参数重新创建它:

static Expression<Func<T, bool>> CreateExpression<T>(Expression<Func<T, int>> parameter, FilterOperator @operator, int value)
{
    var memberExpr = (MemberExpression)parameter.Body;
    PropertyInfo property = (PropertyInfo)memberExpr.Member;

    var argExpr = Expression.Parameter(typeof(T), "p");
    var propertyExpr = Expression.Property(argExpr, property);
    var constExpr = Expression.Constant(value);

    Expression compExpr = null;
    switch(@operator)
    {
        case FilterOperator.GreaterThan:
            compExpr = Expression.GreaterThan(propertyExpr, constExpr);
            break;
        case FilterOperator.LessThan:
            compExpr = Expression.LessThan(propertyExpr, constExpr);
            break;
    }

    return Expression.Lambda<Func<T, bool>>(compExpr, argExpr);
}