Expression. Alternatively, the argument 'item' is out of scope.

c# expression expression-trees linq

Question

The following problem occurs when I attempt to construct a static function to combine two expressions:

The parameter 'item' is not in scope.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The parameter 'item' is not in scope.

the approach:

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);
}

edit: providing additional details

The approach below produces the expressions that are being or'd, and it works just well. If there is a better way to do it or improve the outcomes, I'm all ears. I'm also not sure how many people are being scheduled in advance.

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);
            }

edit: providing further details

Is there another method to perform a or, as an alternative? at this time the. When the constraint is an expression, where(constraint) works well. How can I go from (constraint 1) or (constraint 2) to then'th constraint?

I appreciate it.

1
8
1/4/2009 10:23:21 PM

Accepted Answer

The problem is that the two expressions' bodies are reused when you create an expression using the OrExpressions function. References to their own ParameterExpression, which was specified in FilterExpression, will be found in those bodies.

Rewriting the left and right portions to utilize the new ParameterExpression would be a fix. or to continue using the initial ParameterExpression The two ParameterExpressions do not, despite their names, refer to the same parameter.

9
1/4/2009 10:23:03 PM

Popular Answer

As previously said, you can find this really good (functioning) code at here.

public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
{
    var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
    return Expression.Lambda<Func<T, bool>>(Expression.Or(expr1.Body, invokedExpr), expr1.Parameters);
}

Anything you may customize to your requirements and that isn't (in my opinion) bound to LINQ.



Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow