Using .NET Framework 4.7.2, I have a function that returns a Tuple and the returned values are then passed to another function in a foreach loop:
foreach ((var price, var discount) in Calculator.Compute(period, calculationComponent))
{
var rows = CreateCalculationRows(price, discount, otherParameters);
}
The Compute function is declared as follow:
public static IEnumerable<(Price price, DiscountInfo discount)> Compute(Price price, CalcComponent calcComponent) {
...
}
The code works fine, but I want to remove the foreach loop, since the Calculator.Compute() function, under some specific conditions that are always true in this specific case, returns a single row. I cannot simplify the Compute() return object, because it is used in many other functions.
How do I change the code to remove the foreach, so that the CreateCalculationRows() receives the parameters correctly ?
var (price, discount) ..., which is slightly shorter)FirstorFirstOrDefaultor something, but I think it would be a better idea to create a new method for Compute that only returns a single tuple instead of an IEnumerable to keep your code self-describing.