A read-heavy dashboard repeatedly executes the same EF Core LINQ query with only parameter differences. How do you leverage compiled queries to cut CPU usage?

.NET interview question for Advanced practice.

Answer

Capture the LINQ expression once, pass it to EF.CompileQuery, store the returned delegate, and invoke it with whatever parameters the dashboard needs. EF now skips rebuilding SQL every time and SQL Server can still reuse the plan because parameters remain. BenchmarkDotNet or Application Insights will show the drop in CPU and allocations.

Explanation

Compiled queries skip EF Core's expression-tree translation phase, which dramatically lowers allocations on hot paths.

Related Questions