CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Map same field twice in CSV output

Open bbarvish opened this issue 10 years ago • 6 comments

I have POCO that i'm mapping to a CSV output and I want the same field from the POCO to be used twice in the output but in different ways. For eg:

Map(m => m.PlateNumber).Name("PlateNumber"); Map(m => m.PlateNumber).Name("VehicleType").TypeConverter<PlateToVehicleTypeMapper>();

The output ignores the first mapping of the PlateNumber field even through i'm pushing out to different fields. The second instance of the field works just fine. I've also tried reversing the order and it seems that the Map(ing) instance with the TypeConverter always wins. I've also tried with Index but have not been successful.

Any thoughts?

bbarvish avatar Apr 27 '16 14:04 bbarvish

Currently it only allows for a property to be mapped once. Any subsequent property mappings will overwrite the previous.

I could look into doing this in the future though.

JoshClose avatar Apr 27 '16 16:04 JoshClose

I tried to map

Map(m => m.Size).Name("size");
Map(m => m.Todo).Name("serviceName");
Map(m => m.Todo).Name("version");
Map(m => m.Date).Name("date");

and got exception that property can be used only once. Is it possible to use property 2 times? I found next solution https://github.com/JoshClose/CsvHelper/issues/180#issuecomment-21146510, but it could be easier things work with mapping.

Or if next to work

Map(m => m.Size).Name("size");
Map(m => "Todo").Name("serviceName");
Map(m => "Todo").Name("version");
Map(m => m.Date).Name("date");

I need this for quick iteration with product owners about unknown data sources.

dzmitry-lahoda avatar May 18 '16 11:05 dzmitry-lahoda

Josh is pretty good about merging well written PRs from what I've seen. Doesn't seem like it should be too difficult of you need it so urgently. Just change the internal implementation of the property maps holder to contain multiple per key and write a few tests. Unfortunately that could be a breaking change in some corner cases for current users.

You might also consider convertusing instead of you're in a hurry. I think that should let you map the same field twice. On May 18, 2016 7:30 AM, "Dzmitry Lahoda" [email protected] wrote:

I tried to map

Map(m => m.Size).Name("size"); Map(m => m.Todo).Name("serviceName"); Map(m => m.Todo).Name("version"); Map(m => m.Date).Name("date");

and got exception that property can be used only once. Is it possible to use property 2 times? I found next solution #180 (comment) https://github.com/JoshClose/CsvHelper/issues/180#issuecomment-21146510, but it could be easier things work with mapping.

Or if next to work

Map(m => m.Size).Name("size"); Map(m => "Todo").Name("serviceName"); Map(m => "Todo").Name("version"); Map(m => m.Date).Name("date");

I need this for quick iteration with product owners about unknown data sources.

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/JoshClose/CsvHelper/issues/515#issuecomment-219999341

jamesbascle avatar May 18 '16 11:05 jamesbascle

I’m also having a situation where I need to map the same field twice (or even more then twice) in the CSV output.

@bbarvish Have you find any final solution to this problem?

@JoshClose Any take on when or if this will be implemented in a next release?

juswes avatar May 20 '16 11:05 juswes

This feature is complete and will be in 3.0 beta 7 on NuGet.

JoshClose avatar Sep 30 '16 02:09 JoshClose

I am using v12 and this is still a problem. @JoshClose

pizzo00 avatar Sep 16 '19 14:09 pizzo00