EF 4.1: Why does turning a constant into a variable result in extra sub query?

后端 未结 4 1080
失恋的感觉
失恋的感觉 2021-01-06 09:51

Today I discovered that Entity Framework was adding an unnecessary sub query to the SQL it generates. I started digging my code trying to narrow down where it might come fro

4条回答
  •  走了就别回头了
    2021-01-06 10:18

    The answer is fairly simple. Your LINQ query is expressed with expression trees. The difference with the const variable vs non const one are lies in ConstantExpression and ParameterExpression.

    When you use const your LINQ query uses ConstExpression for this variable, and when you use non const it uses ParameterExpression which are interpreted differently by the EF Runtime.

    Constant actually means that the value will never change and the value can be inlined into the query.

提交回复
热议问题