public static Tuple <List <ResultRow>, List <string> > MapSourceRowsToTargetRows( List <string> listVersions, Dictionary <string, Dictionary <DateTime, string> > versionMapping, List <string> versionColumnsPresent, List <SourceRow> sourceRows) { // Convert source rows to target rows Dictionary <uint, List <ResultRow> > resultRowsByCustomerId = new Dictionary <uint, List <ResultRow> >(sourceRows.Count); foreach (SourceRow sourceRow in sourceRows) { List <ResultRow> resultRowsPerCustomer; if (!resultRowsByCustomerId.TryGetValue(sourceRow.CustomerNumber, out resultRowsPerCustomer)) { resultRowsPerCustomer = new List <ResultRow>(1); resultRowsByCustomerId.Add(sourceRow.CustomerNumber, resultRowsPerCustomer); } ResultRow resultRow = new ResultRow { CustomerNumber = sourceRow.CustomerNumber, PerformanceFullDate = sourceRow.PerformanceDate, PerformanceDate = sourceRow.PerformanceDate.ToString("MM/dd") }; string version = CsvParsingHelper.DetermineVersion( sourceRow.CustomerNumber, !string.IsNullOrEmpty(sourceRow.List1), !string.IsNullOrEmpty(sourceRow.List2), !string.IsNullOrEmpty(sourceRow.List3), !string.IsNullOrEmpty(sourceRow.List4), !string.IsNullOrEmpty(sourceRow.List5), listVersions); resultRow.Version = version; if (versionMapping != null && versionMapping.TryGetValue(version, out Dictionary <DateTime, string> dateMapping) && dateMapping.TryGetValue(resultRow.PerformanceFullDate, out string mappedValue)) { resultRow[version] = mappedValue; } resultRow.YearsSubscribed = sourceRow.YearsSubscribed; resultRow.LetterSalutation = sourceRow.LetterSalutation; resultRow.FullName = CsvParsingHelper.ConstructFullName(sourceRow.FirstName, sourceRow.LastName); CsvParsingHelper.DetermineLocation(sourceRow, resultRow); resultRowsPerCustomer.Add(resultRow); } List <ResultRow> resultRows = new List <ResultRow>(resultRowsByCustomerId.Count); foreach (List <ResultRow> resultRowsPerCustomer in resultRowsByCustomerId.Values) { resultRowsPerCustomer.Sort(new ResultRowImportanceComparer()); resultRows.Add(resultRowsPerCustomer[0]); } resultRows.Sort(new ResultRowLocationComparer()); // Output target rows into a new CSV List <string> targetRows; if (versionColumnsPresent != null) { targetRows = new List <string>(ResultRow.BaseRows.Count + versionColumnsPresent.Count); targetRows.AddRange(ResultRow.BaseRows); targetRows.AddRange(versionColumnsPresent); } else { targetRows = ResultRow.BaseRows; } return(new Tuple <List <ResultRow>, List <string> >(resultRows, targetRows)); }