Skip to content

Instantly share code, notes, and snippets.

@M-Yankov
Last active February 25, 2017 19:56
Show Gist options
  • Save M-Yankov/907cdcacda2b4acf8ec35a7218cfd495 to your computer and use it in GitHub Desktop.
Save M-Yankov/907cdcacda2b4acf8ec35a7218cfd495 to your computer and use it in GitHub Desktop.
Do not use var
/* The code is get from project */
protected void btnExportCSV_Click(object sender, EventArgs e)
{
//Get List of all AnimalData from DAL as a List object
var listAnimalData = AdminMCCDAL.GetActiveAnimalData(); // !!!!
if (listAnimalData.Count > 0)
{
//Export this list of AnimalData to a .CSV file
listAnimalData.ExportCSV("MCC_AnimalIntakesExport");
}
}
public static void ExportCSV<T>(this List<T> list, string filename)
{
ReplaceCarriageReturns(ref list);
string csv = GetCSV(list);
OutputCSV(csv, filename);
}
public static string GetCSV<T>(this List<T> list)
{
var sb = new StringBuilder();
//Get the properties for type T for the headers
var propInfos = typeof(T).GetProperties().ToList(); // typeof(T) problem what the hell is T?
propInfos.ForEach(o => sb.Append(o.Name + ","));
sb.AppendLine();
//Loop through the collection, then the properties and add the values
list.ForEach(o =>
{
propInfos.ForEach(p =>
{
sb.Append(o.GetType().GetProperty(p.Name).GetValue(o, null).ToStringSafe() + ",");
});
sb.AppendLine();
});
return sb.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment