我正在尝试修改动态构建Contains表达式的表达式树,该表达式最终会产生像
这样的SQL P IN (123, 124, 125, 200, 201)
而是检查执行范围检查,最终导致SQL如
(P >= 123 AND P <= 125) OR (P >= 200 AND P <= 201)
我的解决方案基于this post。
static public Expression<Func<TElement, bool>>
BuildContainsExpression<TElement, TValue>(
Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
{
// Removed for post: Input checks and edge cases
var equals =
values.Select(value =>
(Expression)Expression.Equal(valueSelector.Body,
Expression.Constant(value, typeof(TValue))));
var body = equals.Aggregate<Expression>((accumulate, equal) =>
Expression.Or(accumulate, equal));
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
如果我提供比较值,我可以让范围检查工作:
long testValue = 5;
List<KeyValuePair<int, int>> ranges = new List<KeyValuePair<int, int>>()
{
new KeyValuePair<long, long>(3, 6),
new KeyValuePair<long, long>(10, 12),
new KeyValuePair<long, long>(20, 20),
};
List<BinaryExpression> rangeExpressions = new List<BinaryExpression>();
foreach (var pair in ranges)
{
var greaterThanOrEqual =
Expression.GreaterThanOrEqual(Expression.Constant(testValue),
Expression.Constant(pair.Key));
var lessThanOrEqual =
Expression.LessThanOrEqual(Expression.Constant(testValue),
Expression.Constant(pair.Value));
var inRange = Expression.AndAlso(greaterThanOrEqual, lessThanOrEqual);
rangeExpressions.Add(inRange);
}
var final =
rangeExpressions.Aggregate<Expression>((a, b) => Expression.Or(a, b));
var result = Expression.Lambda<Func<bool>>(final).Compile()();
但是,当我将该代码放入要与Linq一起使用的方法时,我无法理解如何从传入的表达式中获取用于比较的值。该方法的签名是:
Expression<Func<TElement, bool>>
BuildRangeExpression<TElement>(
Expression<Func<TElement, long>> valueSelector,
IEnumerable<long> values)
它的使用方式如下:
Expression<MyType, bool> match =
BuildRangeExpression<MyType, long>(my => my.ProductCode, productCodes);
var result = db.MyTypes.Where(match);
问题
我如何评估
Expression<Func<TElement, long>> valueSelector
这样我就可以使用传递给BuildRangeExpression
的值而不是我当前的硬编码值
long testValue = 5;
答案 0 :(得分:3)
我认为博客文章中的代码正是您所需要的:您所要做的就是使用valueSelector.Body
而不是Expression.Constant()
,并将原始参数添加到生成的表达式中: / p>
public static Expression<Func<TElement, bool>>
BuildRangeExpression<TElement, TValue>(
Expression<Func<TElement, TValue>> valueSelector,
IEnumerable<Tuple<TValue, TValue>> values)
{
var p = valueSelector.Parameters.Single();
var equals = values.Select(
tuple =>
Expression.AndAlso(
Expression.GreaterThanOrEqual(
valueSelector.Body, Expression.Constant(tuple.Item1)),
Expression.LessThanOrEqual(
valueSelector.Body, Expression.Constant(tuple.Item2))));
var body = equals.Aggregate(Expression.OrElse);
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
答案 1 :(得分:1)
使用Expression.Parameter
。
创建参数:
var param = Expression.Parameter(typeof(TElement), "arg")
而不是Expression.Constant(testvalue)
,您需要放置param
。
然后,您需要这样做:
var result = Expression.Lambda<Func<TElement, bool>>(final, param).Compile()