4 ms·
It dont think C# looks at the code? I suspect it can track that you called p.Name, then generate sql with this information?
by javier2 1y ago
It dont think C# looks at the code? I suspect it can track that you called p.Name, then generate sql with this information?
- adzm 1y agoThe lambda is converted into an Expression, basically a syntax tree, which is then analyzed to see what is accessed.
- javier2 1y agoOk, so its a step in the compile that rewrites and analyzes it?
- adzm 1y agoIt's actually really neat. Normally a lambda would just be a function but if it gets cast to an Expression then you get the AST at runtime. Entity Framework analyzes these at runtime to generate the SQL and logic/mapping etc. You can get the AST at compile time in some situations with a Roslyn plug-in or etc I believe as well.
- ziml77 1y agoThe C# compiler is looking at that code. It sees that the lambda is being passed into a function which accepts an Expression as a parameter, so it compiles the lambda as an expression tree rather than behaving like it's a normal delegate.