CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Inverse mapping is incorrect

Open brendan87 opened this issue 9 years ago • 2 comments

Steps to reproduce:

  1. Create a mapper using the .Index() extension, representing only a subset of the csv data (e.g. only map index 1, 3, 5)
  2. Write the record back to csv again

Expected result Will write back empty columns for non-represented fields such that index order is preserved

Actual result Writes back columns in order that they are mapped, without padding ignored columns

brendan87 avatar Sep 27 '17 04:09 brendan87

It just sorts by index and outputs in that order. I guess I always assumed that no index would be missing. I'll have to think if I want to allow this, or if I just consider it improperly mapped.

In 3.0 you can map without a property and set a constant to it for the blank rows you want.

Map( m => m.Id ).Index( 0 );
Map().Index( 1 ).Constant( null );
Map( m => m.Name ).Index( 2 );
Map().Index( 3 ).Constant( null );
Map( m => m.Date ).Index( 4 );

JoshClose avatar Sep 27 '17 06:09 JoshClose

I think the example I gave is the expected way to do it. If you're writing, and have a custom mapping, you need to be explicit on your intentions. That was always the intended way. I'll make sure to put some documentation around this.

JoshClose avatar Oct 02 '17 15:10 JoshClose