Interview frequency: …
Short answer
They compile a LINQ expression tree into a ready-to-use delegate, avoiding the cost of analyzing and translating the same LINQ query to SQL on repeated calls.
Explanation
EF Core caches generated SQL queries, but it still spends a small amount of time matching the LINQ expression tree and building the execution plan on every call. With EF.CompileAsyncQuery(), the query is compiled once in a static field and executed directly, approaching the response times of micro-ORMs such as Dapper on frequently used critical paths.
Code example
private static readonly Func<AppDbContext, int, Task<User?>> GetUserCompiled =
EF.CompileAsyncQuery((AppDbContext db, int id) =>
db.Users.AsNoTracking().FirstOrDefault(u => u.Id == id));
// High-performance usage:
var user = await GetUserCompiled(dbContext, userId);Common mistakes
- Compiling every application query in advance without a real need, adding complexity when it is useful only for Hot Paths.