LINQ (Language Integrated Query) changed the way we process data forever. It allowed us to write SQL-like queries directly against C# collections. But under the hood, LINQ is powered by Expression Trees and Iterators.
Lambda expressions were created to make delegates concise. The arrow => literally means "goes to."
// Evolution:
// 1. Named Method: bool IsAdult(User u) { return u.Age > 18; }
// 2. Anonymous Method: delegate(User u) { return u.Age > 18; }
// 3. Lambda: u => u.Age > 18
This is the most critical distinction in LINQ. One runs in RAM, the other runs in the Database.
| Interface | Evaluation | Logic Location |
|---|---|---|
| IEnumerable | Immediate / Deferred | C# RAM (Local) |
| IQueryable | Deferred | SQL Server (Remote) |
LINQ methods like .Where() and .Select() are not actually part of the List class! They are Extensions defined in the System.Linq namespace. That is why you must always have using System.Linq; at the top of your file.
Q: "What is an Expression Tree, and how does EF Core use it to generate SQL?"
Architect Answer: "When you pass a lambda to `IQueryable`, the compiler doesn't generate IL code. Instead, it generates an `Expression Tree`—a data structure representing the logic of your code. EF Core reads this 'Tree' like a map (e.g., 'A GreaterThan property Age') and uses a 'Query Provider' to translate those nodes into raw SQL strings. This is why you can't use complex C# methods inside an EF Core Where clause—the SQL provider doesn't know how to translate your custom C# method nodes into SQL nodes."