public void RecalculatePrices() { BindList <ComplexRecipeDetail> products = detailsProd; double totalMat = detailsMat.Sum(t => t.Total); double totalProd = products.Sum(t => t.Quantity * t.PriceOut); bool allPriceOutAreZero = false; if (totalProd.IsZero() && products.Count > 0) { allPriceOutAreZero = products.All(t => t.PriceOut <= 0); if (allPriceOutAreZero) { totalProd += products.Sum(t => t.Quantity); } } foreach (ComplexRecipeDetail detail in products.Where(p => p.Quantity != 0)) { detail.OriginalPriceInEvaluate(totalProd.IsZero() ? 0 : (allPriceOutAreZero ? 1 : detail.PriceOut) * totalMat / totalProd); detail.TotalEvaluate(); } }