JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.79k stars 1.07k forks source link

[Feature request] Anonymous type and dictionary support #2263

Open alexeygritsenko opened 5 months ago

alexeygritsenko commented 5 months ago

Hi, there is a cool idea that you can borrow from MiniExcel

1. Anonymous type support

csv.WriteRecords(new[] {
    new { Column1 = "MiniExcel", Column2 = 1 },
    new { Column1 = "Github", Column2 = 2}
});

2. Dictionary support

var values = new List<Dictionary<string, object>>()
{
    new Dictionary<string,object>{{ "Column1", "MiniExcel" }, { "Column2", 1 } },
    new Dictionary<string,object>{{ "Column1", "Github" }, { "Column2", 2 } }
};
csv.WriteRecords(values);

Source sample data:

Column1 Column2
MiniExcel 1
Github 2
JoshClose commented 5 months ago

Your anonymous type example currently works as written.

I'd have to look into the dictionary support. You can pass in a dictionary, but not a list of dictionaries. Theoretically this could be done because that's essentially what an ExpandoObject is.