Building a LINQ expression tree: how to get variable in scope

后端 未结 1 1278
小鲜肉
小鲜肉 2021-02-03 22:54

I\'m building a LINQ expression tree but it won\'t compile because allegedly the local variable $var1 is out of scope:

variable \'\' of t

相关标签:
1条回答
  • 2021-02-03 23:19

    Your'e adding the Expression.Variable to the list of "normal" expressions in the block - you should use the overload which specifies the variables do declare for the block separately:

    Expression finalExpression = Expression.Block(new[] { resultVariable },
                                                  expressions);
    

    (And remove the call to expressions.Add(resultVariable);)

    0 讨论(0)
提交回复
热议问题