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
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.