Last active
August 29, 2015 13:57
-
-
Save jacobmendoza/9666896 to your computer and use it in GitHub Desktop.
Fast CSV parsing for NopCommerce provinces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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