This question is not related to how closures work. This question is about how LINQ decides what to quote into a runtime parsable expression, and what to evaluate and put into that expression.
This question is trying to understand how LINQ works, to implement something similar in another language.
Consider the following LINQ query, to be converted into an expression tree:
var my_variable = "abc";
var qry = from x in source.Foo
where x.SomeProp == my_variable
select x.Bar;
which is mapped by the compiler into code:
var qry = source.Foo
.Where(x => x.SomeProp == my_variable)
.Select(x => x.Bar);
When this is converted to an expression tree, how does LINQ know which to quote into expressions, and which to evaluate and put results into the expressions?
For example, how does it know to evaluate my_variable
and put the result into the expressino, but convert x.SomeProp
and ==
into parts of the LINQ Expression Tree?
Does the C# compiler have a hard-coded special list of which expressions are quoted for LINQ? (i.e. the outer-most operations which can be translated into SQL, including: ==, !=, &&, ||, <, >, <=, >=, substring, etc)