3

ASP.Net MVC 中有一个DisplayNameFor(x=>x.Title)助手。我想在行为上实现类似的东西。

我想要一个方法接受基于User类(u=>u.Birthdate或 u=>u.Name)的表达式、一个操作数(Greater、Less、Equal)和一个类似的值DateTime.Now并返回一个表达式 u=>u.Birthdate > DateTime.Now

我知道我必须从片段中手动构建结果表达式。我无法理解的是传递和处理属性表达式。

编辑:
我想调用类似
GetFilterPredicate(u=>u.Birthdate,FilterOps.GreaterThan,DateTime.Parse("01.01.2013") 或的方法
GetFilterPredicate(u=>u.SomeIntProperty,FilterOps.Equals,2)

更新:我创建了一个包含此问题的解决方案的存储库以及一个集合属性过滤 https://github.com/Alexander-Taran/Lambda-Magic-Filters

4

4 回答 4

5

这是否满足您的需求?

[TestClass]
public class UnitTest1
{
    public Expression<Predicate<T>> GetFilterPredicate<T, R>(Expression<Func<T, R>> selector, FilterOps operand, R value)
    {
        var parameter = selector.Parameters[0];

        var left = selector.Body;
        var right = Expression.Constant(value);

        var binaryExpression = Expression.MakeBinary(operand.ToExpressionType(), left, right);
        return Expression.Lambda<Predicate<T>>(binaryExpression, parameter);
    }

    [TestMethod]
    public void TestMethod1()
    {
        var p1 = this.GetFilterPredicate((User u) => u.Birthday.TimeOfDay.Hours, FilterOps.LessThan, 12);
        var p2 = this.GetFilterPredicate((User u) => u.Size, FilterOps.Equal, 180);

        var user = new User() { Birthday = new DateTime(2000, 1, 1), Size = 180 };

        Assert.IsTrue(p1.Compile()(user));
        Assert.IsTrue(p2.Compile()(user));
    }
}

public enum FilterOps
{
    GreaterThan, LessThan, Equal
}
public static class MyExtensions
{
    public static ExpressionType ToExpressionType(this FilterOps operand)
    {
        switch (operand)
        {
            case FilterOps.GreaterThan: return ExpressionType.GreaterThan;
            case FilterOps.LessThan: return ExpressionType.LessThan;
            case FilterOps.Equal: return ExpressionType.Equal;
            default: throw new NotSupportedException();
        }
    }
}

public class User { public DateTime Birthday { get; set; } public int Size { get; set; } }
于 2013-11-05T13:48:17.923 回答
3

我相信这就是你的目标。

public Func<User, bool> MyMethod<TProperty>(Expression<Func<User, TProperty>> func, ComparisonPredicate op, TProperty value)
{


}

public enum ComparisonPredicate
{
    Equal,
    Unequal,
    LessThan,
    LessThanOrEqualTo,
    GreaterThan,
    GreaterThanOrEqualTo
}
于 2013-11-05T12:58:02.240 回答
0

下面的方法将生成一个具有布尔返回值的二进制表达式

Expression<Predicate<TObject>> 
     GenerateAssignExpression<TObject, TProperty>(
         Expression<Func<TObject, TProperty>> expression,
         ExpressionType op, 
         TProperty Value)
    {   
        return Expression.Lambda<Predicate<TObject>>(
            Expression.MakeBinary(op, expression, Expression.Constant(Value)),
            getExpression.Parameters[0]);
    }

如果您希望使用通用返回类型,Predicate<TObject>只需Func<TObject,TReturn>

于 2013-11-05T13:57:45.097 回答
0

像这样的东西:

Expression<Func<TObject, TProperty>> GenerateAssignExpression<TObject, TProperty>
    (Expression<Func<TObject, TProperty>> getExpression,
    TProperty Value)
{
    var getExpressionBody = getExpression.Body as MemberExpression;
    if (getExpressionBody == null)
    {
        throw new Exception("getExpressionBody is not MemberExpression: " + 
                getExpression.Body);
    }

    var objectParameter = (ParameterExpression)getExpression.Parameters[0];
    ConstantExpression constant = Expression.Constant(Value, typeof(TProperty));
    var expAssign = Expression.Assign(e.Body, constant);

    return Expression.Lambda<Func<TObject, TProperty>>(expAssign, 
            objectParameter, 
            valueParameter);
}
于 2013-11-05T13:01:46.943 回答