How does C# code get turned into SQL? The answer is Expression Trees. It's the most powerful (and most misunderstood) feature of the .NET language.
Normally, code is compiled into IL and executed. An Expression Tree is code that is stored as a Data Structure. This allows providers (like Entity Framework) to 'Read' your lambda expression and translate it into something else (like SQL, or even XML).
// This is a delegate (compiled code)
Func<int, bool> isEven = x => x % 2 == 0;
// This is an Expression Tree (data structure describing the code)
Expression<Func<int, bool>> isEvenExpr = x => x % 2 == 0;
If you are building a dynamic reporting engine, a generic repository, or a highly-reusable filter system, you will eventually need to build Expression Trees manually. This allows you to generate dynamic WHERE clauses at runtime based on user input without writing messy string-based SQL.
Q: "Is building Expression Trees slow?"
Architect Answer: "The **creation** of the tree is fast. The **compilation** (converting it back to a delegate) is expensive. If you are building dynamic filters, let the LINQ provider (EF Core) handle the translation. If you must compile at runtime, be sure to use a Cache to store the resulting delegate so you only pay the compilation price once."