我正在尝试以编程方式生成一个表达式树(最终将在 Linq-to-entity 框架中使用)。
我可以让查询正常工作,但有一个例外 - 它不会参数化查询 - 我希望 Sql Server 查询计划重用。
我读到,为了将生成的 sql 参数化,表达式需要基于变量进行比较。但是,我无法弄清楚如何将值分配给表达式树中的变量。如果我只是使用Expression.Constant
它可以工作(但没有参数化)。
所以基本上:
public Expression<Func<T, bool>> FooEquals<T>(
Expression<Func<T, int>> propertyExpression, int value)
{
ParameterExpression param = propertyExpression.Parameters.Single();
int targetValueForEqualityComparison = 9;
//There's some "special sauce" here which is why I don't
//use propertyExpression directly
var body = Expression.Property(param, GetPropertyName(propertyExpression));
//If I just use Expression.Constant, it works, but doesn't parametrize.
//var equalExpression = ParameterExpression.Equal(body,
// Expression.Constant(targetValueForEqualityComparison, typeof(int)));
var variable = Expression
.Variable(typeof(int), "targetValueForEqualityComparison");
var assigned = Expression.Assign(variable,
Expression.Constant(targetValueForEqualityComparison, typeof(int)));
//throws InvalidOperaitonException: "The parameter was not bound in the
//specified Linq to Entities query expression
var equalExpression = ParameterExpression.Equal(body, variable);
//throws NotSupportedException: "Unknown LINQ expression of type 'Assign'.
var equalExpression = ParameterExpression.Equal(body, assigned);
return Expression.Lambda<Func<T, bool>>(equalExpression, param);
}
如何正确地将值绑定到变量表达式,以便 Linq-to-EntityFramework 将对查询进行参数化?