private void RenameStation(object sender, EventArgs e) { var existingStationName = getCmbItemKey(cmbStation.SelectedItem); tbStationRename.Text = _textInfo.ToTitleCase(tbStationRename.Text.ToLower()); var newStationName = tbStationRename.Text + " [" + tbSystemRename.Text + "]"; List<CsvRow> newRows = new List<CsvRow>(); foreach (var row in StationDirectory[existingStationName]) { CsvRow newRow = new CsvRow { BuyPrice = row.BuyPrice, Cargo = row.Cargo, CommodityName = row.CommodityName, Demand = row.Demand, DemandLevel = row.DemandLevel, SampleDate = row.SampleDate, SellPrice = row.SellPrice, StationName = tbStationRename.Text, StationID = newStationName, Supply = row.Supply, SupplyLevel = row.SupplyLevel, SystemName = tbSystemRename.Text, SourceFileName = row.SourceFileName }; newRows.Add(newRow); } StationDirectory.Remove(existingStationName); if (!StationDirectory.ContainsKey(newStationName)) StationDirectory.Add(newStationName, newRows); else StationDirectory[newStationName].AddRange(newRows); var newCommodityDirectory = new CommodityDirectory(); foreach (var collectionOfRows in CommodityDirectory) { newRows = new List<CsvRow>(); foreach (var row in collectionOfRows.Value) { CsvRow newRow = new CsvRow { BuyPrice = row.BuyPrice, Cargo = row.Cargo, CommodityName = row.CommodityName, Demand = row.Demand, DemandLevel = row.DemandLevel, SampleDate = row.SampleDate, SellPrice = row.SellPrice, StationID = row.StationID == existingStationName ? newStationName : row.StationID, Supply = row.Supply, SupplyLevel = row.SupplyLevel, SystemName = row.StationID == existingStationName ? tbSystemRename.Text : row.SystemName }; newRows.Add(newRow); } newCommodityDirectory.Add(collectionOfRows.Key, newRows); } CommodityDirectory = newCommodityDirectory; //Remove duplicates (incl. historical) var newStationDirectory = new List<CsvRow>(); var distinctCommodityNames = StationDirectory[newStationName].ToList().Select(x => x.CommodityName).Distinct(); foreach (var c in distinctCommodityNames) { newStationDirectory.Add(StationDirectory[newStationName].ToList().Where(x => x.CommodityName == c).OrderByDescending(x => x.SampleDate).First()); } StationDirectory[newStationName] = newStationDirectory; var newCommodityDirectory2 = new CommodityDirectory(); for (int i = 0; i < CommodityDirectory.Keys.Count; i++) { var distinctStationNames = CommodityDirectory.ElementAt(i).Value.Select(x => x.StationID).Distinct(); foreach (var d in distinctStationNames) { if (!newCommodityDirectory2.Keys.Contains(CommodityDirectory.ElementAt(i).Key)) newCommodityDirectory2.Add(CommodityDirectory.ElementAt(i).Key, new List<CsvRow> { CommodityDirectory.ElementAt(i).Value.ToList().Where(x => x.StationID == d).OrderByDescending(x => x.SampleDate).First() }); else newCommodityDirectory2[CommodityDirectory.ElementAt(i).Key].Add(CommodityDirectory.ElementAt(i).Value.ToList().Where(x => x.StationID == d).OrderByDescending(x => x.SampleDate).First()); } } CommodityDirectory = newCommodityDirectory2; _StationHistory.RenameStation(existingStationName, newStationName); SetupGui(); }