using System.Collections;
using System.Collections.Generic;
using System.Globalization;
public static void Main()
var records = new List<dynamic>{
(new Dictionary<string, object>{{"firstName", "Fred"}, {"lastName", "Flintstone"}}).ToExpando(),
(new Dictionary<string, object>{{"firstName", "Barnie"}, {"lastName", "Rubble"}}).ToExpando()};
using (var writer = new StreamWriter(Console.OpenStandardOutput()))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
csv.WriteRecords(records);
public static class Extensions
public static ExpandoObject ToExpando(this IDictionary<string, object> dictionary)
var expando = new ExpandoObject();
var expandoDic = (IDictionary<string, object>)expando;
foreach (var kvp in dictionary)
if (kvp.Value is IDictionary<string, object>)
var expandoValue = ((IDictionary<string, object>)kvp.Value).ToExpando();
expandoDic.Add(kvp.Key, expandoValue);
else if (kvp.Value is ICollection)
var itemList = new List<object>();
foreach (var item in (ICollection)kvp.Value)
if (item is IDictionary<string, object>)
var expandoItem = ((IDictionary<string, object>)item).ToExpando();
itemList.Add(expandoItem);
expandoDic.Add(kvp.Key, itemList);