# Exercise: SnackShack

Answer a snack bar owner's questions with LINQ. What's cheap, what's in each category, what sells best, what never sells, how much came in each day, and one page of the menu at a time, each in one chain of methods.

Journeyman · C# · Updated October 30, 2026

The Snack Shack sells chips, cookies and apples from a counter. Its owner keeps the menu and every sale in lists, and has questions: what's cheap, what sells best, what never sells, and how much money came in on Wednesday. You'll answer each one with LINQ.

## Get the exercise code

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-snackshack](https://github.com/ask-codey/exercise-snackshack), also linked from the **Exercise** box beside the lesson. Click **Code**, then **Download ZIP**, and unzip it into your `bootcamp` folder. Or clone it with Git. Then open `SnackShack.slnx` in Visual Studio.

| File | What's in it |
|---|---|
| `SnackShack/Snack.cs`, `Sale.cs` | A snack on the menu, and one sale of it |
| `SnackShack/SampleData.cs` | The menu and a week of sales, for the program |
| `SnackShack/SnackReport.cs` | The owner's questions, for you to answer. Its requirements are in the comment at the top. |
| `SnackShack.Tests` | Tests for `SnackReport`, with their own small menu |

## What to do

Write each method of `SnackReport` with LINQ. Most are a single chain of methods, without a `foreach`.

| Method | Answers |
|---|---|
| `UnderPrice(maxPrice)` | Snacks at or under a price, cheapest first, ties by name |
| `NamesInCategory(category)` | The names in one category, alphabetical |
| `FindByName(name)` | One snack, whatever the capitals, or `null` |
| `AllUnder(calories)` | Is every snack under that many calories? |
| `TotalItemsSold()`, `Revenue()`, `RevenueOn(day)` | Items sold, and money taken, overall or on one day |
| `BestSeller()` | The snack with the most items sold |
| `NeverSold()` | Snacks on the menu nobody bought |
| `CategorySummary()` | One line per category: items sold and money taken |
| `Page(pageNumber, pageSize)` | One page of the menu, alphabetical |

Run the program with **Ctrl+F5** to see the week's report, and the tests with **Test > Run All Tests**.

## Hints

- **Start with the one-step answers:** `NamesInCategory` is `Where`, `Select`, `OrderBy`, `ToList`; `TotalItemsSold` is one `Sum`.
- **Money from a sale** is `sale.Snack.Price * sale.Quantity`. Put that in a `Sum`.
- **`FindByName`** is `FirstOrDefault`, comparing names with `Equals(name, StringComparison.OrdinalIgnoreCase)` so capitals don't matter.
- **`BestSeller` and `CategorySummary`** start with `GroupBy`. Then each group's `Sum(sale => sale.Quantity)` is its items sold, and `group.Key` its name or category.
- **`NeverSold`** asks, for each snack on the menu, whether no sale is of it: `!sales.Any(...)` inside a `Where`.
- **`Page`:** page 3, with 4 snacks to a page, skips the first 8 snacks, then takes 4.

## When you're done

Every test passes, and the program prints:

```text
$1.50 or less:
  Banana $0.75
  Apple $0.90
  Cookie $1.25
  Chips $1.50
  Granola Bar $1.50
Sweet snacks: Brownie, Candy Bar, Cookie
Items sold: 26
Revenue: $40.65 (Wednesday: $13.25)
Best seller: Chips
Never sold: Granola Bar, Pretzels
Healthy: 4 sold, $4.65
Salty: 13 sold, $22.50
Sweet: 9 sold, $13.50
```

Count the `foreach` loops in `SnackReport`. If the answer is zero, every question became one readable sentence.

## Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.

1. On GitHub, click **+**, then **New repository**. Name it (for example `exercise-snackshack`), choose **Public**, and click **Create repository**. Leave out the README: your folder already has one.
2. In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. If you downloaded the ZIP:

   ```bash
   git init -b main
   git add .
   git commit -m "My solution"
   git remote add origin https://github.com/your-name/exercise-snackshack.git
   git push -u origin main
   ```

   If you cloned the starter instead, point your copy at your own repo, then commit and push:

   ```bash
   git remote set-url origin https://github.com/your-name/exercise-snackshack.git
   git add .
   git commit -m "My solution"
   git push -u origin main
   ```

3. Paste your repo's address, such as `https://github.com/your-name/exercise-snackshack`, into **Submit your exercise** on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

## So what

Each question the owner asked became a single chain: where, order, select, sum, group. That's LINQ's promise: say what you want, and let the methods do the looping.

## Key terms

- [LINQ](https://www.ask-codey.com/glossary/linq)
- [Lambda Expression](https://www.ask-codey.com/glossary/lambda-expression-c-sharp)
- [Where()](https://www.ask-codey.com/glossary/where)
- [GroupBy()](https://www.ask-codey.com/glossary/groupby)

## Related

- [Common LINQ Methods](https://www.ask-codey.com/lessons/linq-common-linq-methods)
- [Lambda Expressions](https://www.ask-codey.com/lessons/linq-lambda-expressions)

Source: https://www.ask-codey.com/lessons/exercise-snackshack
