Interview Questions: LINQ, EF Core and ASP.NET Core

Dot NET Programming 22 min min read Updated: Nov 16, 2026 Advanced
Interview Questions: LINQ, EF Core and ASP.NET Core
Advanced Topic 2 of 3

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()).

csharp var query = numbers.Where(n => n > 5); // nothing runs yet numbers.Add(10); var list = query.ToList(); // runs now, includes 10

2. First vs FirstOrDefault vs Single vs SingleOrDefault?

MethodNo matchMany matches
FirstThrowsReturns first
FirstOrDefaultReturns null/defaultReturns first
SingleThrowsThrows
SingleOrDefaultReturns null/defaultThrows

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

csharp // N+1: 1 query for orders + 1 query per order for customer (lazy loading or loop) foreach (var o in await db.Orders.ToListAsync()) Console.WriteLine(o.Customer.Name); // Fix 1: eager loading var orders = await db.Orders.Include(o => o.Customer).ToListAsync(); // Fix 2: projection (best for read models) var rows = await db.Orders.Select(o => new { o.Id, Customer = o.Customer.Name }).ToListAsync();

7. Include vs projection vs explicit loading?

  • Include: load related entities (use when you need the entities)
  • Select projection: 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?

LifetimeInstanceUse for
TransientNew every timeLight, stateless services
ScopedOne per requestDbContext, unit-of-work services
SingletonOne for the appCaches, configuration, stateless helpers (must be thread-safe)
Captive dependency: A singleton must not hold a scoped service. It would keep one 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?

ActionCode
GET success200
POST created201 + Location
PUT/DELETE success, no body204
Bad input400 / 422
Not authenticated / not allowed401 / 403
Not found404
Conflict409

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?

  • AsNoTracking and 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, Stopwatch or 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.

Quick Summary: LINQ deferred, EF tracking and N+1, DI lifetimes, middleware order, JWT, performance.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators