Home
Options

List of ExpandoObjects in Util.WriteCsv

Hi Joe,

The latest version handles ExpandoObjects well with Dump, which is sweet as I had to do a nasty select with them to get the proper results before. However what works well with Dump still does not work well with Util.WriteCsv. The result is this:
Value
System.Dynamic.ExpandoObject
System.Dynamic.ExpandoObject
System.Dynamic.ExpandoObject...

Any ideas how to get the same output as Dump?

Thanks,

Gergely

Comments

  • Options
    edited March 2019
    I was hoping for an answer to this question as well.

    In the end I just wrote my own, so for anyone looking for this, just cast your ExpandoObjects to IDictionary and pass the list:
    private void WriteLine(StreamWriter outputFile, IEnumerable<string> items) { var first = true; foreach (var item in items) { if (!first) outputFile.Write(","); if (null != item) outputFile.Write(item); else outputFile.Write(string.Empty); first = false; } outputFile.WriteLine(); } public void WriteCsv(IList<IDictionary<string, object>> results, string outputFilePath) { using (var outputFile = new StreamWriter(outputFilePath)) { // First write the headers WriteLine(outputFile, results.First().Keys); // Now write the data rows foreach (var result in results) { WriteLine(outputFile, result.Values.Select(v => v.ToString())); } } }
  • Options
    Good call - I'll fix this for the next build.
Sign In or Register to comment.