Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is probably the lowest-tech answer here, but why not just use a switch and merge the multiple "GetTotal...Amount" functions? </p> <pre><code> // define some enum for your callers to use public enum AmountTypeEnum { ReleasedFederal = 1 , ReleasedLocal = 2 } public decimal GetTotalAmountByDate(AmountTypeEnum type) { if (BeginDate == null &amp;&amp; EndDate == null) return 0; decimal total = 0; foreach (var fi in Financials) { // declare a variable that will hold the amount: decimal amount = 0; // here's the switch: switch(type) { case AmountTypeEnum.ReleasedFederal: amount = fi.ReleasedFederalAmount; break; case AmountTypeEnum.ReleasedLocal: amount = fi.ReleasedLocalAmount; break; default: break; } // continue with your processing: if (someCondition) if (someSubCondition) total += amount; else if (someOtherCondition) if (someOtherSubCondition) total += amount; else if (anotherCondigion) total += amount; } return total; } </code></pre> <p>This seems safer, because all your logic remains under your control (no one is passing you functions to execute).</p> <p>This can be further broken down if you need to actually do different things with different amounts:</p> <p>Take the processing part and turn it into a function:</p> <pre><code> private decimal ProcessNormal(decimal amount) { decimal total = 0; // continue with your processing: if (someCondition) if (someSubCondition) total += amount; else if (someOtherCondition) if (someOtherSubCondition) total += amount; else if (anotherCondition) total += amount; return total; } public decimal GetTotalAmountByDate(AmountTypeEnum type) { if (BeginDate == null &amp;&amp; EndDate == null) return 0; decimal total = 0; foreach (var fi in Financials) { // declare a variable that will hold the amount: decimal amount = 0; // here's the switch: switch(type) { case AmountTypeEnum.ReleasedFederal: amount = fi.ReleasedFederalAmount; total = ProcessNormal(amount); break; case AmountTypeEnum.ReleasedLocal: amount = fi.ReleasedLocalAmount; total = ProcessNormal(amount); break; case AmountTypeEnum.NonReleasedOtherAmount: amount = fi.NonReleasedOtherAmount; total = ProcessSlightlyDifferently(amount); // for argument's sake break; default: break; } } return total; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload