Skip to content

1. ExportData

emmateva18 edited this page Jul 14, 2021 · 3 revisions

Functions:

ListToCsv<T> function

Parameters:

  • IEnumerable<T> list
  • string path
  • string separator

What does it do?

First we get the data properties from a list.
var props = t.GetProperties();

This function creates a StringBuilder, in which we save first the column names separated by a delimiter
var names = props.Select(prop => prop.Name);
sb.AppendLine(string.Join(separator, names));

and then the data in the fields as string.
var values = props.Select(prop => { return prop.GetValue(item)?.ToString(); });
sb.AppendLine(string.Join(separator, values));

Then it saves everything in a file.
File.WriteAllText(path, sb.ToString());

Clone this wiki locally