我正在尝试将静态函数写入或两个表达式,但收到以下错误:
参数“项目”不在范围内。
说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.InvalidOperationException:参数“项目”不在范围内。
方法:
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)
提前致谢!