Filehelpers: Class containing Dictionary to csv

39 views Asked by At

I am trying to export csv file using FileHelpers engine. The class has Dictionary property. Each dictionary key should be separate column in a csv file

e.g.

public class Customer
{
    public int Id { get; init; }
    public string Name { get; init; }
    public Dictionary<string, string> Props { get; init; }

}
Id Name Prop1 Prop2
1 James Val 1.1 Val 1.2
2 John Val 2.1 Val 2.2

Props count may vary from case to case, but there will be same number of dictionary entries for each particular case so it is easy to make columns.

running the simple code

var records = new[]
{
    new Customer(1, "James", new()
    {
        { "Property1", "PropertyValueForJames" },
        { "Property2", "PropertyValueForJames" }
    }),
    new Customer(1, "John", new()
    {       
        { "Property1", "PropertyValueForJohn1" },
        { "Property2", "PropertyValueForJohn2" }
    }),
};

var engine = new FileHelperEngine<Customer>();
engine.WriteFile(@"c:\temp\Output.Txt", records);

results in exception

FileHelpers.BadUsageException: 'The field: 'Props' has the type: Dictionary`2 which is not a system type, so this field needs a CustomConverter (please check the docs for more info).'

I tried to google the solution but seems there is not much on this topic. Just curious if there is a straight forward / or best practice on how to do this? May be some dynamic options.

0

There are 0 answers