Skip to content
Learn Netverks

Lesson

Step 20/36 56% through track

linq-basics

LINQ basics

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

This lesson

This lesson teaches LINQ basics: the syntax, patterns, and safety habits you need before advancing in C#.

LINQ is idiomatic C#—interviewers ask about deferred execution, IEnumerable, and when not to hammer the database.

You will apply LINQ basics in contexts like: Reporting queries, in-memory transforms, and EF Core projections in enterprise apps.

Write C# with Console.WriteLine (top-level or Program), 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.

LINQ (Language Integrated Query) queries collections with a fluent API—Where, Select, OrderBy—similar to Java Streams but built into C# syntax with query expressions. This is a major differentiator from Java and JavaScript array methods alone.

Method syntax

var nums = new[] { 1, 2, 3, 4, 5 };
var evens = nums.Where(n => n % 2 == 0);
var squares = evens.Select(n => n * n);

LINQ operators return IEnumerable<T> and execute lazily until you enumerate or call aggregators like Count, Sum, or ToList.

Query syntax (optional)

var query = from n in nums where n % 2 == 0 select n * n;

Important interview questions and answers

  1. Q: Deferred execution?
    A: LINQ chains build an expression tree/iterator; work runs when you foreach or materialize—re-enumerating may re-run filters.
  2. Q: LINQ to Objects vs EF?
    A: In-memory collections here; EF translates LINQ to SQL in the ASP.NET data lessons.

Self-check

  1. Which operator filters elements?
  2. When does a Where chain actually run?

Tip: LINQ queries are deferred—materialize with ToList() or ToArray() when you need a snapshot or multiple enumerations.

Interview prep

Deferred execution?

LINQ operators build a query pipeline—enumeration (foreach, ToList) runs the query; multiple enumerations may re-run it.

Where vs Select?

Where filters elements; Select projects each element to a new shape.

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

  • Deferred execution?
  • Where vs Select?

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