从BinaryExpression到Expression< Func< T,bool> [英] From BinaryExpression to Expression<Func<T, bool>>

查看:402
本文介绍了从BinaryExpression到Expression< Func< T,bool>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有类似的东西

Expression<Func<SomeType, DateTime>> left = x => x.SomeDateProperty;
Expression<Func<SomeType, DateTime>> right = x => dateTimeConstant;
var binaryExpression = Expression.GreaterThan(left, right);
Expression<Func<SomeType, bool>> predicate = 
                          x => x.SomeDateProperty> dateTimeConstant;

1)如何用使用binaryExpression的内容替换最后一行分配的右手? var predicate = x => binaryExpression;不起作用.

1) How can I replace the right hand of the assignment of the last line with something that uses the binaryExpression instead? var predicate = x => binaryExpression; doesn't work.

2)right始终是常量,不一定是DateTime.Now.可以是一些更简单的Expression类型吗?例如,它不依赖SomeType,它只是一个常量.

2) The right is always a constant, not necessarily DateTime.Now. Could it be of some simpler Expression type? For instance, it doesn't depend on SomeType, it is just a constant.

3)如果我将GreaterThan作为string,是否有办法将这个字符串转换为与Expression中同名的方法?通常,如果比较方法的名称以string给出,那么如何从字符串转换为在Expression类上实际调用具有相同名称的方法?

3) If I have the GreaterThan as a string, is there a way to get from this string to the method with the same name in Expression? In general, if the name of the comparison method is given as a string, how can I go from the string to actually calling the method with the same name on the Expression class?

如果重要的话,它必须与LINQ to Entities合作.

It has to work with LINQ to Entities, if it matters.

推荐答案

1和2:您需要手动构建表达式树才能执行此操作,编译器无济于事,因为它仅构造表示函数全部.当您要逐步构建函数时,这没什么用.

1 and 2: You need to build the expression tree manually to do this, the compiler cannot help because it only constructs ready-made expressions that represent functions in their entirety. That's not useful when you want to build functions piece by piece.

这是一种构建所需表达式的简单方法:

Here's one straightforward way to build the expression you want:

var argument = Expression.Parameter(typeof(SomeType));
var left = Expression.Property(argument, "SomeDateProperty");
var right = Expression.Constant(DateTime.Now);

var predicate = Expression.Lambda<Func<SomeType, bool>>(
    Expression.GreaterThan(left, right),
    new[] { argument }
);

您可以将其用于试驾

var param = new SomeType { 
    SomeDateProperty = DateTime.Now.Add(TimeSpan.FromHours(-1))
};

Console.WriteLine(predicate.Compile()(param)); // "False"

3:由于您的二元谓词可能的选择数量将非常少,因此您可以使用字典来做到这一点:

3: Since in all likelihood the number of possible choices for your binary predicate will be quite small, you could do this with a dictionary:

var wordToExpression = 
    new Dictionary<string, Func<Expression, Expression, BinaryExpression>>
{
    { "GreaterThan", Expression.GreaterThan },
    // etc
};

然后,而不是在第一个片段中对Expression.GreaterThan进行硬编码,您将执行类似wordToExpression["GreaterThan"](left, right)的操作.

Then, instead of hardcoding Expression.GreaterThan in the first snippet you would do something like wordToExpression["GreaterThan"](left, right).

当然,这也可以通过反射的标准方式完成.

Of course this can also be done the standard way with reflection.

这篇关于从BinaryExpression到Expression&lt; Func&lt; T,bool&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆