/// <summary> /// Reads the data for this import from the file specified. /// </summary> /// <param name="filename">The file to read from.</param> /// <param name="errorMessage">The error message if unsuccessful.</param> /// <returns>True if read successfully, false otherwise.</returns> public override bool ReadFromFile(string filename, out string errorMessage) { _constituencyResultProvider = new ConstituencyResultProvider(); errorMessage = string.Empty; // Try to deserialize the file into the constituency results. List <ConstituencyResult> constituencyResults = new List <ConstituencyResult>(); try { using (StreamReader sr = new StreamReader(filename, Encoding.Default)) { CsvReader csv = new CsvReader(sr, CultureInfo.CurrentCulture); bool foundStartDataTag = false; while (csv.Read()) { // Get the first field. string onsCode = csv.GetField <string>(0); // Ignore empty entries. if (string.IsNullOrWhiteSpace(onsCode)) { continue; } // If haven't found the header line see if this is it if (!foundStartDataTag) { if (onsCode.Contains(DataStartTag)) { foundStartDataTag = true; } continue; } // If here on a line with data convert it into a record. constituencyResults.Add( GetConstituencyResultFromRecord(csv, onsCode, PartyNames)); } } } catch (Exception e) { errorMessage = e.ToString(); return(false); } if (constituencyResults.Any()) { _constituencyResultProvider = new ConstituencyResultProvider(constituencyResults); } return(true); }
private static ConstituencyResultProvider GetConstituencyResultProviderWithSwings( IConstituencyResultProvider provider, Dictionary <string, float> swingsByParty) { List <ConstituencyResult> resultsWithSwings = new List <ConstituencyResult>(); foreach (string constituencyName in provider.ConstituencyNames) { resultsWithSwings.Add( new ConstituencyResult(provider.ResultsByName[constituencyName], swingsByParty)); } ConstituencyResultProvider predictedResultProvider = new ConstituencyResultProvider(resultsWithSwings); return(predictedResultProvider); }