forked from JoshClose/CsvHelper
-
Notifications
You must be signed in to change notification settings - Fork 1
New Property Mapping Features "ConvertFieldUsing"
thiscode edited this page Apr 19, 2013
·
1 revision
The Property-Mapping-Feature "ConvertFieldUsing" is just a shortcut for "ConvertUsing". "ConvertUsing" is perfect if you want to combine two columns into one resulting value. But the most times i have used "ConvertUsing" i need only one field to lead through a special method, so i did write "ConvertFieldUsing". The parameter of the delegate is not the whole row, it's only the field you have maped:
//Instead of this
Map(m => m.IntColumn).ConvertUsing<int>(
x => Convert.ToInt32(Math.Round(x.GetField<decimal>("Height", DecimalAllowThousands), 0))
);
//You can write this
Map(m => m.IntColumn).Name("Height").ConvertFieldUsing<int>(
x => Convert.ToInt32(Math.Round((decimal)DecimalAllowThousands.ConvertFromString(x), 0))
);