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 shows the SQL it generates on the sample tests, with its query count and timing, and 5 of the 8 also capture the plan.

No signup or setup. Try Project Only the Names, then explore the full Pro path.

Katabench results after a real Submit on the Project Only the Names kata, of a version that calls ToList before Select. The verdict reads Failed: query plan rule, with 4 / 4 tests passed and 3 / 5 query-plan rules holding. Under Your query plan, the rules for a single SQL statement, a direct customer scan followed by the sort, and ordering the names in the database pass, while two red rules say the root plan may output at most 1 column and the query must return name directly.
A real Submit on Project Only the Names: every test passes, and two query plan rules still fail the solution.
guided LINQ exercises
8
database challenges
206
free to try
6

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.

Section 1

Shape the query on the server

Stop fetching rows and fixing the result in application memory.

  1. Easy Free

    Filter Before You Fetch

    Move the predicate before materialization.

    Read the brief: Filter Before You Fetch
  2. Easy Free

    Project Only the Names

    Select only the column the caller needs.

    Read the brief: Project Only the Names
  3. Easy Free

    Existence Without Loading

    Use an existence query when the count is irrelevant.

    Read the brief: Existence Without Loading
  4. Medium Free

    Customers Without Orders

    Express absence without loading both sides.

    Read the brief: Customers Without Orders

Section 2

Aggregate with intent

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

  1. Medium Free

    Customer Order Totals

    Group and sum without falling back to client evaluation.

    Read the brief: Customer Order Totals
  2. Medium Free

    Top Spending Customers

    Aggregate, order, and limit as one server query.

    Read the brief: Top Spending Customers
  3. Medium Pro

    Repeat Customers (Pro: opens the Learning Path)

    Filter groups with more than one qualifying row.

  4. Medium Pro

    Region Revenue Ranking (Pro: opens the Learning Path)

    Group by a related column and rank the totals in one query.

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. Read IQueryable vs IEnumerable for where that boundary sits and multiple enumeration in LINQ for what deferred execution costs.

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 Count

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(t => t.TappedAt.AddHours(offset).Year == year && t.TappedAt.AddHours(offset).Month == month)

Sort (by id)

Seq Scan on taps

60,000 rows read · ⚠ full table read

  • ✗ No full-table scan on taps
  • ✗ Uses ix_taps_tapped_at
  • ✓ One round-trip

✗ 1 / 3 plan rules hold

The sargable rewrite

.Where(t => t.TappedAt >= start && t.TappedAt < end)

Sort (by id)

Index Scan on taps using ix_taps_tapped_at

100 rows read · ✓

  • ✓ No full-table scan on taps
  • ✓ Uses ix_taps_tapped_at
  • ✓ 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

Sign in to 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. Revealing needs an account, and daily reveal limits depend on your plan.

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, and timing, plus the execution plan on katas with plan rules, rather than mocking database behavior.

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

Make the query show its work

6 of these 8 exercises are free, no signup needed. Pro adds the complete 19-exercise path through LINQ, SQL, query plans, indexes, and analytical queries.

Try a free LINQ exercise