Eager vs Explicit Loading
As a Tech Lead engineer, explain Eager vs Explicit Loading. What problem does it solve and how would you describe it in an interview?
Answers use simple, clear English.
Quick interview answer
At Tech Lead depth: start with the problem, then mechanism, then a short example. Include/ThenInclude eager-loads related data in one query.
Detailed answer
At Tech Lead depth: start with the problem, then mechanism, then a short example. Include/ThenInclude eager-loads related data in one query. Explicit loading: reference/load after query. Avoid lazy loading in web apps (N+1 in loops). Core: Include/ThenInclude eager-loads related data in one query. Explicit loading: reference/load after query. Avoid lazy loading in web apps (N+1 in loops). Real-time example: Order list with items: .Include(o => o.Items) prevents N+1 when rendering line counts. Pros: Include gives predictable SQL; projection Select reduces over-fetching. Cons: Cartesian explosion with multiple Includes — use split queries. Common mistakes: Lazy loading enabled globally causing hidden N+1 in foreach. Best practices: Prefer Select projection DTOs; AsSplitQuery for multiple collections. Audience level: Tech Lead.
Full explanation
Include/ThenInclude eager-loads related data in one query. Explicit loading: reference/load after query. Avoid lazy loading in web apps (N+1 in loops).
Real example & use case
Order list with items: .Include(o => o.Items) prevents N+1 when rendering line counts.
Pros & cons
Pros: Include gives predictable SQL; projection Select reduces over-fetching. Cons: Cartesian explosion with multiple Includes — use split queries.
Common mistakes
Lazy loading enabled globally causing hidden N+1 in foreach.
Best practices
Prefer Select projection DTOs; AsSplitQuery for multiple collections.
Follow-up questions
- How would you test Eager vs Explicit Loading?
- What metrics prove Eager vs Explicit Loading is healthy in prod?
- How does Eager vs Explicit Loading change at 10× traffic?