lucasteles42

C# Csv

Sep 15th, 2020
676
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   public static string ObterCsv<T>(IEnumerable<T> items, string separador = ";") {
  2.  
  3.      var props = typeof(T).GetProperties();
  4.      var header = string.Join(separador, props.Select(p => p.Name));
  5.  
  6.      string gerarLinha(T item) =>
  7.                      props
  8.                      .Select(p => p.GetValue(item)?.ToString() ?? string.Empty)
  9.                      .Aggregate((acc, n) => $"{acc}{separador}{n}");
  10.  
  11.       return $"{header}\n{string.Join("\n", items.Select(gerarLinha))}";
  12.   }
RAW Paste Data