static string CalculateCheapestMonth(ListNodes<Residents> residentsListNodes, ListNodes<Utilities> utilitiesListNodes) { var cheapestMonth = new Dictionary<string, Dictionary<string, double>>(); var cheapestMonthListNodes = new ListNodes<CheapestMonth>(); var temp = residentsListNodes; for (residentsListNodes.StartingNode(); residentsListNodes.Contains(); residentsListNodes.RightNode()) { if (cheapestMonth.ContainsKey(residentsListNodes.GetData().Month)) { if (cheapestMonth[residentsListNodes.GetData().Month].ContainsKey(residentsListNodes.GetData().UtilityCode)) { cheapestMonth[residentsListNodes.GetData().Month][residentsListNodes.GetData().UtilityCode] += FindUtilityPrice(utilitiesListNodes, residentsListNodes.GetData().UtilityCode) * residentsListNodes.GetData().ServiceCount; } else { cheapestMonth[residentsListNodes.GetData().Month].Add( residentsListNodes.GetData().UtilityCode, FindUtilityPrice(utilitiesListNodes, residentsListNodes.GetData().UtilityCode) * residentsListNodes.GetData().ServiceCount ); } } else { cheapestMonth.Add(residentsListNodes.GetData().Month, new Dictionary<string, double>()); cheapestMonth[residentsListNodes.GetData().Month].Add( residentsListNodes.GetData().UtilityCode, FindUtilityPrice(utilitiesListNodes, residentsListNodes.GetData().UtilityCode) * residentsListNodes.GetData().ServiceCount ); } } foreach (KeyValuePair<string, Dictionary<string, double>> item in cheapestMonth) { foreach (KeyValuePair<string, double> utility in cheapestMonth[item.Key]) { cheapestMonthListNodes.AddData(new CheapestMonth(item.Key, utility.Key, utility.Value)); } } var cheapestMonthUtility = cheapestMonthListNodes.OrderBy(x => x.Price).FirstOrDefault(); for (utilitiesListNodes.StartingNode(); utilitiesListNodes.Contains(); utilitiesListNodes.RightNode()) { if (cheapestMonthUtility.UtilityCode == utilitiesListNodes.GetData().ServiceCode) { cheapestMonthUtility.UtilityCode = utilitiesListNodes.GetData().ServiceName; } } if (cheapestMonthUtility != null) { return $"{cheapestMonthUtility.Month} mėnesį {cheapestMonthUtility.UtilityCode} kainavo pigiausiai"; } return "Nerasta"; }