Setting column order for CSVHelper

You can just use the Index attribute(CsvHelper.Configuration.Attributes.IndexAttribute, Assembly: CsvHelper) instead. here

public class Foo
{
    [Index(0)]
    public int Id { get; set; }

    [Index(1)]
    public string Name { get; set; }
}

Take a look at the Mapping section of the website for CSVHelper (http://joshclose.github.io/CsvHelper/2.x/)

Specifically:

When mapping by index you specify the index of the CSV column that that you want to use for that property

So you'll have to specify a mapping class for your NodeDPCount class, telling it which index to use for which records.

public sealed class MyNodeDPCountMap : CsvClassMap<NodeDPCount>
{
    public MyNodeDPCountMap()
    {
        Map( m => m.Id ).Index( 0 );
        Map( m => m.Name ).Index( 1 );
        // etc.
    }
}

For this to work, you'll need to register your map:

csv.Configuration.RegisterClassMap<MyNodeDPCountMap>();

Then it will know to use the map you've registered when interacting with the NodeDPCount class

Tags:

C#

Csvhelper