Here's a messy C# method used to calculate some business logic.
Does anybody have any suggestions on how I can optimise this code? I'm asking in case there are bad practices, improvements or functions within .NET/LINQ which I'm missing.
private static decimal GetRecurringCostEquiv(List<QuotationItemViewModel> items)
{
var discVal = GetDiscounts(items, (int)RecordEnums.ProductClassification.Service);
decimal? currentRecurringCosts = GetRecurringCost(items).Value;
decimal? result = 0.00M;
decimal highestContractTerm = items.Where(x => x.ProductTypeId == (int)RecordEnums.ProductClassification.Service).Select(y => y.RecurringTerm).Max() ?? 0;
if ((discVal > 0) && (highestContractTerm > 0))
{
var calc = items.Where(x => x.ProductTypeId == (int)RecordEnums.ProductClassification.Service).Select(y => y.RecurringCostCore * y.Quantity);
decimal total = calc.Sum() ?? 0;
decimal totalOverTerm = total * highestContractTerm;
decimal totalOverTermMinusDiscounts = totalOverTerm - discVal;
result = totalOverTermMinusDiscounts / highestContractTerm;
}
else
{
result = currentRecurringCosts;
}
return result ?? 0.00M;
}
GetDiscounts()
,GetRecurringCost()
\$\endgroup\$GetRecurringCost()
returns a decimal? \$\endgroup\$decimal?
. \$\endgroup\$