Skip to content

Instantly share code, notes, and snippets.

@jacobmendoza
Last active August 29, 2015 13:57
Show Gist options
  • Save jacobmendoza/9666896 to your computer and use it in GitHub Desktop.
Save jacobmendoza/9666896 to your computer and use it in GitHub Desktop.
Fast CSV parsing for NopCommerce provinces
// This script assumes unique Abbreviation field.
const int countryId = 72;
var sql =
@"IF NOT EXISTS (SELECT 1 FROM StateProvince WHERE Abbreviation = '{2}')
BEGIN
INSERT StateProvince (CountryId, Name, Abbreviation, Published, DisplayOrder) VALUES ({0}, '{1}', '{2}', {3}, {4})
END;";
var linesOfFile = File.ReadAllLines(@"input.csv", Encoding.Default);
var lines = linesOfFile
.Select(l => l.Split(';'))
.Select(t => new {Name = t[1], Abbreviation = t[0]})
.OrderBy(t => t.Name)
.Select((t,i) => string.Format(sql, countryId, t.Name, t.Abbreviation, 1, i + 1))
.ToArray();
File.WriteAllLines("output.sql", lines);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment