A query describes which values contribute to an answer
Filtering and aggregation solve different parts of a question. Where selects values that satisfy a predicate, while Sum combines the selected numeric values. The example asks for the sum of prices that are at least ten. Keeping the threshold rule separate from the addition lets you inspect both decisions: an incorrect answer may come from selecting the wrong prices or from calculating a different aggregate, and those errors need different evidence.
The array contains decimals because its literals use the m suffix. The lambda p => p >= 10m describes a boolean test for one decimal element. The name p is local to that lambda; it does not refer to a separate stored global price or automatically accumulate a result. using System.Linq makes the extension methods available in this file. It has no output effect, and it does not turn the local array into a database or send its values to a server.
Account for every original element
Read the input in order. Twelve satisfies the inclusive threshold, so it contributes twelve. Eight fails the predicate and contributes nothing. Twenty-five satisfies it and contributes twenty-five. The decimal Sum therefore returns thirty-seven, which is assigned to total and printed as 37. Filtering has not changed eight into zero or deleted it from the original array; it simply does not appear in the sequence consumed by this aggregate.
Where is deferred for this in-memory sequence: creating its query does not immediately store a new filtered array. Sum enumerates the query and produces a single result before WriteLine runs. The chained layout can therefore be read as a sequence of responsibilities rather than as three separate stored collections. The original source array remains twelve, eight, and twenty-five afterward. This example reads it once for the sum and has no side effects in the predicate.
var total = prices
.Where(p => p >= 10m)
.Sum();The threshold’s equality case is part of the requirement
At least ten includes exactly ten. Replacing >= with > changes that boundary, even though the unchanged three inputs do not include ten and would still print thirty-seven. This is why a passing example is not sufficient evidence for every rule. Also distinguish Sum from Count: Count would return the number of selected values, two, rather than their total. Name result variables according to the question they answer so those operations are not accidentally interchanged during a refactor.
Expose a boundary, then compare two aggregates
First change the initial price from twelve to ten, leaving the threshold inclusive. Predict thirty-five, because ten and twenty-five qualify. Now change only the comparison to strictly greater than ten and predict twenty-five. Hint: choose an input on the boundary to reveal the effect of the operator; the original input cannot distinguish the two conditions. Restore the original sample after recording both modified results.
Next store the filtered sequence in a variable and print both its Count and its Sum. With the original data, expect two selected elements and a total of thirty-seven. Add a temporary display of the original array and verify that eight is still present. Hint: enumeration is a read operation here, not a list-removal operation. Finally try a threshold above every price and explain why the decimal sum becomes zero rather than representing a missing execution.
Verify selection independently from arithmetic
For each input price, write whether it passes and how much it contributes. Then add only the accepted values and compare that hand calculation with the output. Include a value exactly at the threshold and one case with no accepted values when testing a reusable rule. Those cases check the selection contract and empty-result behavior, while a separate count helps you distinguish how many observations qualified from how large their total became.
- The original decimal inputs are twelve, eight, and twenty-five.
- At least ten keeps twelve and twenty-five.
- Sum returns
37; Count would return two. - The query does not delete eight from the source array.
A working example
using System.Linq;
var prices = new[] { 12m, 8m, 25m };
var total = prices
.Where(p => p >= 10m)
.Sum();
Console.WriteLine(total);Expected output
37
Filter first. Add only what remains.