public static string ToCsv<T>(this IEnumerable<T> instance, bool includeColumnHeader, string[] properties)
{
if (instance == null)
return null;
var csv = new StringBuilder();
if (includeColumnHeader)
{
var header = new StringBuilder();
foreach (var property in properties)
header.AppendFormat("{0},", property);
csv.AppendLine(header.ToString(0, header.Length - 1));
}
foreach (var item in instance)
{
var row = new StringBuilder();
foreach (var property in properties)
row.AppendFormat("{0},", item.GetPropertyValue<object>(property));
csv.AppendLine(row.ToString(0, row.Length - 1));
}
return csv.ToString();
}
public static string ToCsv<T>(this IEnumerable<T> instance, bool includeColumnHeader)
{
if (instance == null)
return null;
var properties = (from p in typeof(T).GetProperties()
select p.Name).ToArray();
return ToCsv(instance, includeColumnHeader, properties);
}