【发布时间】:2009-01-04 21:44:31
【问题描述】:
我正在尝试将静态函数写入或两个表达式,但收到以下错误:
参数“item”不在范围内。
描述:未处理的异常 在执行过程中发生 当前的网络请求。请查看 堆栈跟踪以获取有关的更多信息 错误及其起源 代码。
异常详情: System.InvalidOperationException: 参数“item”不在范围内。
方法:
public static Expression<Func<T, bool>> OrExpressions(Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
{
// Define the parameter to use
var param = Expression.Parameter(typeof(T), "item");
var filterExpression = Expression.Lambda<Func<T, bool>>
(Expression.Or(
left.Body,
right.Body
), param);
// Build the expression and return it
return (filterExpression);
}
编辑:添加更多信息
or'd 的表达式来自下面的方法,执行得很好。如果有更好的方法或结果我全神贯注。另外,我不知道有多少被提前或被淘汰。
public static Expression<Func<T, bool>> FilterExpression(string filterBy, object Value, FilterBinaryExpression binaryExpression)
{
// Define the parameter to use
var param = Expression.Parameter(typeof(T), "item");
// Filter expression on the value
switch (binaryExpression)
{
case FilterBinaryExpression.Equal:
{
// Build an expression for "Is the parameter equal to the value" by employing reflection
var filterExpression = Expression.Lambda<Func<T, bool>>
(Expression.Equal(
Expression.Convert(Expression.Property(param, filterBy), typeof(TVal)),
Expression.Constant(Value)
),
param);
// Build the expression and return it
return (filterExpression);
}
编辑:添加更多信息
或者,有没有更好的方法来做一个或?目前,.Where(constraint) 在约束类型为 Expression> 的情况下工作得很好。我该怎么做 where(constraint1 or constraint2) (到约束 n'th)
提前致谢!
【问题讨论】:
标签: c# linq expression-trees expression