C# & CLR Internals (50 Qs)•Part 2: Concurrency, Zero-Allocation & Advanced Language Features (11 - 24)
What are Expression Trees and how does LINQ translate C# expressions into SQL?
senior LevelCode ExampleDatabases & SQLData Structures
In Simple Words:
While Func<T, bool> compiles lambda code directly into IL machine bytecode, Expression<Func<T, bool>> represents code as an abstract syntax tree (AST) data structure that can be inspected, traversed, and rewritten at runtime.
csharp
// Linq to Objects (compiles to IL delegate):
Func<User, bool> inMemoryFilter = u => u.Age > 21;
// Linq to Entities (compiles to AST Expression Tree):
Expression<Func<User, bool>> dbFilter = u => u.Age > 21;
SQL Translation:
ORM providers (like Entity Framework Core) traverse the Expression tree nodes (BinaryExpression, MemberExpression) and translate u.Age > 21 into the SQL string WHERE [u].[Age] > 21.