Skip to content
Learn Netverks

Lesson

Step 27/36 75% through track

linq-queries

LINQ queries with EF Core

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches LINQ queries with EF Core: the syntax, APIs, and habits you need before advancing in ASP.NET.

Teams ship LINQ queries with EF Core on every ASP.NET codebase—skipping it leaves gaps in debugging and code reviews.

You will apply LINQ queries with EF Core in contexts like: Line-of-business APIs, intranets, BFF layers, and cloud-hosted services on Linux or Windows.

Write C# (top-level or Program class), click Run on server—the dev runner uses dotnet build/run on a temp net8 project (requires .NET SDK; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

EF Core translates LINQ expressions to SQL. Filter and project in the database—don't fetch entire tables into memory unless intentional.

Common queries

var cheap = await _db.Products
    .Where(p => p.Price < 20)
    .OrderBy(p => p.Name)
    .ToListAsync();

var one = await _db.Products
    .FirstOrDefaultAsync(p => p.Id == id);

Projection

var names = await _db.Products
    .Select(p => new { p.Id, p.Name })
    .ToListAsync();

Project early to reduce columns transferred—especially important for list endpoints.

Important interview questions and answers

  1. Q: IQueryable vs IEnumerable?
    A: IQueryable builds expression trees EF translates to SQL; IEnumerable often means client-side evaluation after ToList.
  2. Q: N+1 problem?
    A: Loading related entities in a loop—fix with Include/ThenInclude or projection.

Self-check

  1. Why call Where before ToListAsync?
  2. What does FirstOrDefault return when no row matches?

Pitfall: Calling ToList() before Where pulls entire tables into memory—filter and project in LINQ so EF translates to SQL server-side.

Interview prep

What is N+1 in EF Core?

One query for a list plus one per row for related data—fix with Include/ThenInclude or project in a single Select query.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • AsNoTracking when?
  • Include N+1 fix?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump