JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.65k stars 1.05k forks source link

Ability to find Index of a field via ClassMap instance #2199

Open jzabroski opened 9 months ago

jzabroski commented 9 months ago

Is your feature request related to a problem? Please describe. I sometimes enhance the output of CsvHelper csv files I create using CsvWriter by creating an Excel file from that csv file.

Describe the solution you'd like

  1. A property on ClassMap called DataModel of type ClassMapDataModel that effectively represents the object model WriteRecord uses.
  2. A method on ClassMapDataModel called GetIndexOf(string name) that would return the index of a column.
  3. For the DataModel to work when using MemberMap, ParameterMap and ReferencesMap items - especially MemberMap and ReferencesMap.

Describe alternatives you've considered Read in the first line of the generated file, and split it by ','. This is not great for many reasons I don't think we need to discuss, lol.

var indexOfQtyColumn = File.ReadLines(csvPath).FirstOrDefault().Split(',')
    .Select((value, index) => new { value, index})
    .Where(pair => pair.value.Contains("Qty"))
    .Select(pair => pair.index)
    .FirstOrDefault();

Additional context I was actually surprised to read the docs carefully and notice References says its for Internal Use Only. I've been using this feature a ton, so I hope it continues to remain public.