You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to create a CSV file which has a dictionary collection, where each key in the dictionary is mapped as separate column headers. This is the best I've come up with so far:
I've tried numerous things but nothing seems to work. Ideally I'd like to use a ClassMap instead of writing the raw contents as I think this should be supported out of the box.
The text was updated successfully, but these errors were encountered:
I'm not sure why I closed this out. For now I got around this by doing the following:
using(varwriter=newStreamWriter("file.csv"))using(varcsv=newCsvWriter(writer,newCsvConfiguration(CultureInfo.InvariantCulture){HasHeaderRecord=false})){csv.Context.RegisterClassMap<ProductViewModelMap>();csv.WriteHeader<ProductViewModel>(["Attribute1","Attribute2"]);awaitcsv.WriteRecordsAsync(products);}```
Here's my extensionmethod to manually write the header:
```cs
publicstaticclassCsvWriterExtensions{publicstaticvoidWriteHeader<T>(thisCsvWritercsv,IEnumerable<string>attributeNames){foreach(varpropertyintypeof(T).GetProperties().Where(p =>p.Name!="Attributes")){csv.WriteField(property.Name);}foreach(varattributeNameinattributeNames){csv.WriteField(attributeName);}csv.NextRecord();}}
My temporary solution: replace classmap with method that converts T to dynamic. + WriteDynamicHeader()
It would great to have requested functionality...
Is it possible to create a CSV file which has a dictionary collection, where each key in the dictionary is mapped as separate column headers. This is the best I've come up with so far:
However this produces:
But I would like:
I've tried numerous things but nothing seems to work. Ideally I'd like to use a
ClassMap
instead of writing the raw contents as I think this should be supported out of the box.The text was updated successfully, but these errors were encountered: