Expression.Or, The parameter 'item' is not in scope

前端 未结 5 1150
一整个雨季
一整个雨季 2021-02-09 00:23

I am trying to write a static function to Or two expressions, but recieve the following error:

The parameter \'item\' is not in scope.

Descrip

5条回答
  •  余生分开走
    2021-02-09 00:51

    I'm not sure about the proper terms here, but basically expression parameters are not equivalent even if they have the same name.

    That means that

    var param1 = Expression.Parameter(typeof(T), "item");
    var param2 = Expression.Parameter(typeof(T), "item");
    
    param1 != param2
    

    param1 and param2 won't be the same thing if used in an expression.

    The best way to deal with this is create one parameter up front for your expression, and then pass it to all helper functions that need the parameter.

    EDIT: Also, if you're trying to dynamically compose where clauses in LINQ, you could give PredicateBuilder a try.

提交回复
热议问题