Exercise 8 of 10 · LINQ
Craft Fair Booth Sales
What you will make
A program that filters a craft fair vendor's sales to the ones above a minimum amount and totals just those.
The one new idea: Using LINQ's Where and Sum to filter and total a list
LINQ lets you express 'filter, then total' directly instead of hand-writing a loop with an if-check and an accumulator every time — a big readability win once you're comfortable with lists.
Go straight to the code ↓You now know how to loop over a list and check a condition on each item by hand. LINQ gives you shorter, more direct ways to express common patterns like that. list.Where(x => condition) returns only the elements for which the lambda expression is true, and .Sum() adds up all the numbers in a sequence. Chaining .Where(...).Sum() reads almost like the sentence you'd use to describe the goal: total up the ones that qualify.
Worked example
Here's a list of race finish times, totaling just the ones under a cutoff:
List<int> finishTimesSeconds = new List<int> { 58, 71, 49, 90, 63 };
int cutoff = 65;
List<int> fastFinishes = finishTimesSeconds.Where(t => t < cutoff).ToList();
int fastTotal = fastFinishes.Sum();
Console.WriteLine($"Fast finishes: {fastFinishes.Count}");
Console.WriteLine($"Combined time: {fastTotal}");Fast finishes: 3
Combined time: 170Where(t => t < cutoff) keeps 58, 49, and 63, the three times under 65, and .Sum() adds just those: 58 + 49 + 63 = 170.
Your turn
The starter code is supposed to filter a craft fair vendor's sales down to just the ones at or above a minimum amount, then total those. But its Where condition is s => s < minimumSale, which keeps the sales below the minimum instead — the exact opposite of what the next line claims to print. Fix the comparison so it keeps sales that are at or above the minimum.
If something goes wrong
If the qualifying count and total look suspiciously like the "wrong half" of the data, re-read your Where lambda out loud — it's easy to write < when you mean >=, since both look like small, easy-to-miss symbols. If you get a compile error about .Count or .Sum() not existing, check whether you kept the .ToList(), or are calling those members on something LINQ already returns directly.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Writing a Where condition that's the logical opposite of what's intended.
- There's no compiler error for this — the filter just silently keeps the wrong elements.
- Forgetting .ToList() after Where when a List<T> (not just a sequence) is needed later.
- Without it, you have a lazy IEnumerable<T> instead of a List<T>, which can behave unexpectedly if reused or checked for .Count.
- Calling .Sum() on the original unfiltered list by accident.
- This silently includes every value instead of just the ones that passed the filter, giving a total that's too high or too low.
Want a blank editor instead? Open the C# playground.