Last active
September 18, 2015 04:00
-
-
Save melvinlee/79165b07920d15936346 to your computer and use it in GitHub Desktop.
Helper method to parse csv into type.
This file contains hidden or 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
| public static class CsvParser | |
| { | |
| private static string CheckSpecialChar(string checkChar) | |
| { | |
| return checkChar.Replace("\"", ""); | |
| } | |
| public static Collection<UploadExtensionModel> ParseExtension(string fileName) | |
| { | |
| string[] lines; | |
| using (var csv = new StreamReader(fileName)) | |
| { | |
| lines = csv.ReadToEnd().Split(Environment.NewLine.ToCharArray()); | |
| } | |
| var extensions = (from p in lines | |
| let properties = Regex.Split(p, @",(?=(?:[^\""]*\""[^\""]*\"")*(?![^\""]*\""))") | |
| where (!string.IsNullOrEmpty(p)) | |
| where (!p.Contains("Ext")) | |
| where (!p.Contains("EXT")) | |
| where (!p.Contains("DEPARTMENT")) | |
| select new UploadExtensionModel | |
| { | |
| Extension = CheckSpecialChar(properties[0]), | |
| Owner = CheckSpecialChar(properties[1] ?? ""), | |
| Pin = CheckSpecialChar(properties[2] ?? ""), | |
| Department = CheckSpecialChar(properties[3] ?? ""), | |
| CostCentre = CheckSpecialChar(properties[4] ?? ""), | |
| }).Distinct(new UploadExtensionCompare()).ToList(); | |
| return new Collection<UploadExtensionModel>(extensions); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment