/// <summary> /// Loads a CSV file containing race entrant information. /// </summary> public dynamic LoadCsvFile(string filename) { // ToDo: Long term, make this more configurable. At the moment we're only interested // in Runner's World files. dynamic result = new ExpandoObject(); result.Imported = 0; result.AlreadyExisting = 0; result.Ignored = 0; using (var reader = new StreamReader(File.OpenRead(filename))) { string line; // Count number of lines - can specify which line throws error var lineCount = 0; // Do process while line is not empty while ((line = reader.ReadLine()) != null) { lineCount++; // If line throws exception then skip try { // Split line into array of info (\\s* removes space around object) var runnerInfo = SplitCSV(line).ToArray(); var runner = new Runner { FirstName = runnerInfo[0].ToUpper(), LastName = runnerInfo[1].ToUpper(), Gender = (runnerInfo[2] == "F") ? GenderEnum.Female : GenderEnum.Male, DateOfBirth = DateParser.ParseRwDate(runnerInfo[4]), Club = RemoveUnwantedAttributes(runnerInfo[5]), Team = RemoveUnwantedAttributes(runnerInfo[6]), Email = (runnerInfo.Count() > 15) ? RemoveUnwantedAttributes(runnerInfo[15]) : null, Urn = (runnerInfo.Count() > 33) ? RemoveUnwantedAttributes(runnerInfo[33]) : null, Number = db.GetNextNumber(), }; var addressFields = runnerInfo .Skip(8).Take(6) .Where(s => !string.IsNullOrEmpty(s)).ToList(); if (addressFields.Any()) { runner.Address = addressFields.Aggregate((current, next) => current + ", " + next); } // If a club is given, then assume the runner is affiliated runner.Affiliated = !string.IsNullOrEmpty(runner.Club); // ToDo: Check for invalid team names - 'NONE' or 'N/A'. // Check that they don't already exist in the DB (use firstname, lastname and DoB) if (!db.TestDuplicate(runner)) { db.AddRunner(runner); result.Imported++; } else { result.AlreadyExisting++; } } catch (Exception ex) { Trace.WriteLineIf(traceSwitch.TraceWarning, string.Format("Error with line {0}: {1}. Line is '{2} ", lineCount, ex.Message, line)); result.Ignored++; } } } return(result); }