Skip to content

Instantly share code, notes, and snippets.

@melvinlee
Last active September 18, 2015 04:00
Show Gist options
  • Select an option

  • Save melvinlee/79165b07920d15936346 to your computer and use it in GitHub Desktop.

Select an option

Save melvinlee/79165b07920d15936346 to your computer and use it in GitHub Desktop.
Helper method to parse csv into type.
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