LINQ
1. What is deferred execution?
A LINQ query is not executed when you write it. It runs when you enumerate it (foreach, ToList(), Count(), First()).
2. First vs FirstOrDefault vs Single vs SingleOrDefault?
| Method | No match | Many matches |
|---|---|---|
First | Throws | Returns first |
FirstOrDefault | Returns null/default | Returns first |
Single | Throws | Throws |
SingleOrDefault | Returns null/default | Throws |
3. Select vs SelectMany?
Select maps each item to one result. SelectMany maps each item to a collection and flattens all collections into one sequence.
4. Any() vs Count() > 0?
Any() stops at the first match and translates to an efficient EXISTS query. Prefer it.
EF Core
5. What is change tracking? When do you use AsNoTracking?
EF Core remembers loaded entities so SaveChanges can detect changes. For read-only queries use AsNoTracking() to save memory and time.
6. Explain the N+1 problem and the fix
7. Include vs projection vs explicit loading?
Include: load related entities (use when you need the entities)Selectprojection: load only needed columns (fastest for APIs)- Explicit loading: load a relation later on demand
8. How do you handle concurrency?
Add a concurrency token ([Timestamp] byte[] or [ConcurrencyCheck]). If another user changed the row, SaveChanges throws DbUpdateConcurrencyException and you can reload, merge or tell the user.
9. How do migrations work?
dotnet ef migrations add Name compares your model with the last snapshot and generates Up and Down code. database update applies it and records it in __EFMigrationsHistory. In production prefer SQL scripts or migration bundles.
10. How do you prevent SQL injection in EF Core?
LINQ queries and FromSql($"...{value}") interpolation are parameterized. Never concatenate user input into SQL strings.
11. ExecuteUpdate / ExecuteDelete?
They run set-based UPDATE and DELETE directly in the database without loading entities. Much faster for bulk operations, but they bypass change tracking and SaveChanges interceptors.
ASP.NET Core
12. DI lifetimes?
| Lifetime | Instance | Use for |
|---|---|---|
| Transient | New every time | Light, stateless services |
| Scoped | One per request | DbContext, unit-of-work services |
| Singleton | One for the app | Caches, configuration, stateless helpers (must be thread-safe) |
DbContext forever. Use IServiceScopeFactory to create a scope.
13. What is middleware and does order matter?
Middleware are components in the request pipeline. Each can act before and after the next one. Order matters: exception handler first, then HTTPS, CORS, authentication, authorization, then endpoints.
14. Middleware vs filters?
Middleware runs for every request. Filters run around endpoints (MVC actions or endpoint filters) and know about model binding and results.
15. Authentication vs authorization? How does JWT work?
Authentication proves identity; authorization checks permissions. The server signs a JWT with a secret key at login. The client sends it in the Authorization: Bearer header. The API validates signature, issuer, audience and expiry on each request. The payload is not encrypted.
16. Minimal APIs vs controllers?
Both are production-ready. Minimal APIs have less ceremony and are great for small services and microservices. Controllers offer conventions, filters and attributes that suit big teams and large APIs. Many apps use both.
17. Which HTTP status codes for CRUD?
| Action | Code |
|---|---|
| GET success | 200 |
| POST created | 201 + Location |
| PUT/DELETE success, no body | 204 |
| Bad input | 400 / 422 |
| Not authenticated / not allowed | 401 / 403 |
| Not found | 404 |
| Conflict | 409 |
18. What is IOptions<T>?
It binds a configuration section to a class. IOptions reads once, IOptionsSnapshot re-reads per request, IOptionsMonitor notifies on changes.
19. How do you improve API performance?
AsNoTrackingand projections- Paging, never return whole tables
- Indexes on filtered and sorted columns
- Async everywhere, avoid blocking calls
- Caching (memory, output, Redis)
- Response compression
- Avoid N+1 queries
- Measure first with logs,
Stopwatchor profilers
20. How do you handle errors globally?
Use AddProblemDetails, UseExceptionHandler and an IExceptionHandler, return consistent ProblemDetails JSON, log the real exception and never leak stack traces to clients.
Conclusion
Backend interviews test practical knowledge: deferred execution, tracking, N+1, DI lifetimes, middleware order, JWT and status codes. Answer with a small example from a project you built.

