Skip to content
Katabench
Try free

C# LINQ practice on a real database

LINQ exercises that make every query prove its work

Practice filtering, projection, existence checks, grouping, and aggregates in your browser. Your C# runs through EF Core against PostgreSQL, so every exercise can show the generated SQL, query count, timing, and plan behind the result.

Nothing to install. Reference solutions are available when you need them.

guided LINQ exercises
8
database challenges
130
mock databases
0

The exercise list

Eight LINQ problems, ordered by the judgment they build

These are the first two sections of the real High-Performance Data Access path. Each starts with working or plausible code that does the wrong amount of work. Your job is to keep the result correct while changing where and how the work happens.

01

Shape the query on the server

Stop fetching rows and fixing the result in application memory.

  1. 1.cs Easy

    Filter Before You Fetch

    Move the predicate before materialization.

  2. 2.cs Easy

    Project Only the Names

    Select only the column the caller needs.

  3. 3.cs Easy

    Existence Without Loading

    Use an existence query when the count is irrelevant.

  4. 4.cs Medium

    Customers Without Orders

    Express absence without loading both sides.

02

Aggregate with intent

Make grouping, totals, and rankings happen in one round trip.

  1. 5.cs Medium

    Customer Order Totals

    Group and sum without falling back to client evaluation.

  2. 6.cs Medium

    Top Spending Customers

    Aggregate, order, and limit as one server query.

  3. 7.cs Medium

    Repeat Customers

    Filter groups with more than one qualifying row.

  4. 8.cs Medium

    Average Measurement Value per Device

    Compute per-device averages without N+1 queries.

What you will practice

Read LINQ as work, not just syntax

Two expressions can return the same rows while producing radically different database work. The exercises train you to predict the boundary before production traffic finds it.

Where before ToList

Only matching rows cross the wire

Filtering stays in SQL instead of loading a table into application memory.

Select the required shape

Generated SQL requests only the needed columns

Less data is transferred and fewer entities are materialized.

Any instead of loading or counting

The database answers one existence question

The query can stop at the first match and return one Boolean value.

Group, Sum, and Average

One translated aggregate query

Reporting work stays set-based and avoids an N+1 round-trip loop.

OrderBy and Take on IQueryable

Ordering and limiting appear in SQL

The database ranks the rows and returns only the requested result window.

Executable feedback

A passing result is only the first check

A static exercise can compare your final rows with an answer. Katabench can also prove that the filter stayed server-side, the aggregate used one round trip, and the access path did not scan work you never asked for.

🧭 Your query plan, captured from the database, graded

The obvious filter

.Where(o => o.PlacedAt.Year == 2024 && o.PlacedAt.Month == 3)

Sort (by id)

Seq Scan on orders

~60,000 rows · ⚠ full table read

  • No full-table scan on orders
  • Uses the placed_at index
  • One round-trip

✗ 1 / 3 plan rules hold

The sargable rewrite

.Where(o => o.PlacedAt >= start && o.PlacedAt < end)

Sort (by id)

Index Scan using ix_orders_placed_at

~1,017 rows · ✓

  • No full-table scan on orders
  • Uses the placed_at index
  • One round-trip

✓ 3 / 3 plan rules hold

Same rows returned. Same green tests. Only one earns the index scan, and the grade.

Hidden datasets

Larger data exposes materializing too early and N+1 loops that look fine on five rows.

Generated SQL

See whether Where, Select, OrderBy, Take, and aggregates translated where you expected.

Reference solutions

Reveal a correct implementation after your attempt, then reset and reproduce it yourself.

LINQ exercise FAQ

What you need before you start

Can I practice these LINQ exercises online?

Yes. You write C# in the browser, then Katabench compiles and runs the solution in an isolated server-side environment. You do not need the .NET SDK or PostgreSQL installed locally.

Are these LINQ-to-Objects or Entity Framework Core exercises?

This sequence focuses on LINQ-to-Entities with EF Core: filtering, projection, existence checks, grouping, aggregates, ordering, and query shaping. The grader shows how the expression translates to SQL and what the database did with it.

Do the LINQ exercises include solutions?

Every exercise includes a correct reference solution you can reveal after attempting it. Hidden tests and database feedback still let you discover why a solution works instead of only copying an answer from a static list.

What level are the exercises?

The guided sequence starts with Easy query-shaping exercises and moves into Medium aggregation and N+1 problems. It assumes you already know basic C# syntax and want practical LINQ and EF Core experience.

Does the grader use a real database?

Yes. Each submission runs against a temporary PostgreSQL database with seeded data. Katabench captures generated SQL, query counts, timing, and execution-plan evidence rather than mocking database behavior.

Want to go deeper after the LINQ sequence? Continue through all 19 exercises in the guided path, or explore the broader EF Core practice catalog.

Make the query show its work

Start with 8 ordered LINQ exercises, then continue through SQL, query plans, indexes, and analytical queries in the complete 19-exercise path.

Start High-Performance Data Access

Get new puzzles and .NET tips in your inbox

A short note when fresh kata land, plus the C# and performance tricks behind the grading. No spam, unsubscribe anytime.