Thuta Learning
C#
AdvancedProgrammingBeginner

LINQ: Querying Collections

C#Lesson 2825 min

What you'll walk away with

  • Explain the core concept and runtime behavior of LINQ: Querying Collections
  • Test normal, boundary, and invalid cases independently
  • Write readable, type-safe, and testable C# code

Explain it simply

LINQ: Querying Collections, in plain language

LINQ lets you write strongly typed queries against collections and other data sources. Where filters, and Select reshapes the data. Until you call ToList() on a query, it can remain a deferred execution.

Problem → Why → Solution

The problem — why this concept matters

Repeatedly enumerating a query or treating database LINQ like local LINQ creates performance problems.

A common mistake

Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Core concept — understand how it works

LINQ filters, transforms, orders, groups, and aggregates strongly typed sequences. Separate query definition from execution and understand deferred execution, repeated enumeration, and the difference between `IEnumerable<T>` and remote `IQueryable<T>` translation.

Mental model
  1. task or collection
  2. .NET API and pipeline
  3. typed result or failure

Explain the core concept and runtime behavior of LINQ: Querying Collections

Core syntax or pattern

csharp
var scores = new[] { 68, 42, 91, 77, 55 };

var passed = scores
    .Where(score => score >= 60)
    .OrderByDescending(score => score)
    .Select(score => $"Score: {score}")
    .ToList();

passed.ForEach(Console.WriteLine);
Console.WriteLine($"Average: {scores.Average():0.0}");

LINQ filters, transforms, orders, groups, and aggregates strongly typed sequences. Separate query definition from execution and understand deferred execution, repeated enumeration, and the difference between `IEnumerable<T>` and remote `IQueryable<T>` translation.

Read the code step by step

  1. task or collectionvar scores = new[] { 68, 42, 91, 77, 55 };

    LINQ lets you write strongly typed queries against collections and other data sources. Where filters, and Select reshapes the data. Until you call ToList() on a query, it can remain a deferred execution.

  2. .NET API and pipeline.Select(score => $"Score: {score}")

    LINQ filters, transforms, orders, groups, and aggregates strongly typed sequences. Separate query definition from execution and understand deferred execution, repeated enumeration, and the difference between `IEnumerable<T>` and remote `IQueryable<T>` translation.

  3. typed result or failureConsole.WriteLine($"Average: {scores.Average():0.0}");

    Filter, order, and project products into a DTO shape, then compare enumeration before and after changing the source.

Execution flow
  1. task or collection
  2. .NET API and pipeline
  3. typed result or failure

Success

Explain the core concept and runtime behavior of LINQ: Querying Collections

Failure

Repeatedly enumerating a query or treating database LINQ like local LINQ creates performance problems.

Let's break it down simply

LINQ lets you write strongly typed queries against collections and other data sources. Where filters, and Select reshapes the data. Until you call ToList() on a query, it can remain a deferred execution.

csharp
var scores = new[] { 68, 42, 91, 77, 55 };

var passed = scores
    .Where(score => score >= 60)
    .OrderByDescending(score => score)
    .Select(score => $"Score: {score}")
    .ToList();

passed.ForEach(Console.WriteLine);
Console.WriteLine($"Average: {scores.Average():0.0}");
You should see
Score: 91
Score: 77
Score: 68
Average: 66.6

Try it yourself

Filter the Products collection by category, sort by price from lowest to highest, then select just the name and price.

Language Integrated Query (LINQ) — Microsoft Learn

Real-world usage

How to apply it in a real project

Filter, order, and project products into a DTO shape, then compare enumeration before and after changing the source.

Mini challenge

Build it yourself

Filter, order, and project products into a DTO shape, then compare enumeration before and after changing the source.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
csharp
var scores = new[] { 68, 42, 91, 77, 55 };

var passed = scores
    .Where(score => score >= 60)
    .OrderByDescending(score => score)
    .Select(score => $"Score: {score}")
    .ToList();

passed.ForEach(Console.WriteLine);
Console.WriteLine($"Average: {scores.Average():0.0}");

Expected: Explain the core concept and runtime behavior of LINQ: Querying Collections

Show hint

Filter, order, and project products into a DTO shape, then compare enumeration before and after changing the source.

Quick quiz 1 / 3

What best describes the core idea of LINQ: Querying Collections?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • task or collection → .NET API and pipeline → typed result or failure
  • LINQ filters, transforms, orders, groups, and aggregates strongly typed sequences. Separate query definition from execution and understand deferred execution, repeated enumeration, and the difference between `IEnumerable<T>` and remote `IQueryable<T>` translation.
  • Repeatedly enumerating a query or treating database LINQ like local LINQ creates performance problems.
  • Filter, order, and project products into a DTO shape, then compare enumeration before and after changing the source.

Language Integrated Query (LINQ) — Microsoft Learn

Easy traps

  • Repeatedly enumerating a query or treating database LINQ like local LINQ creates performance problems.
  • Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Hands-on Exercise

Filter, order, and project products into a DTO shape, then compare enumeration before and after changing the source.

You'll know it worked when: Score: 91 Score: 77 Score: 68 Average: 66.6

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

LINQ: Querying Collections | Thuta Learning