One line turns the whole battery green. The exercise stubs the cube's single primitive to return 0; here it is filled in, and every cell, total and grand total lights up — because they were all built on top of it.

The one line

Cell is the value at a coordinate: the sum of every fact whose Position and Year both match.

double Cell(string position, string year) =>
    facts.Where(f => f.Position == position && f.Year == year).Sum(f => f.Amount);

That is the entire pivot engine. RowTotal, ColTotal and Grand were already written in terms of Cell, so the moment Cell is right they are right too:

You wrote one grouping rule and got cells, row totals, column totals and the grand total for free. Nothing was entered twice. That is the claim of Lesson 1, made concrete.

Live and green

Fill in the Cell(position, year) primitive — the sum of every fact whose Position and Year both match — so the whole battery goes green: cells, row totals, column totals and the grand total of 2202.

The takeaway: a pivot is not a special data structure — it is one grouping primitive, folded along whichever dimension you point it at.


← Back to the exercise · Lesson 1 · All solutions

Reconnecting…
The connection to the server was interrupted. Trying to restore it…
Trying again…
The connection could not be restored. Reloading the page…
The server was updated. Reloading the page to pick up the latest version.