Csvhelper: Suggestion: ShouldSkipRecord overload which supports header names

Created on 4 Apr 2018  路  2Comments  路  Source: JoshClose/CsvHelper

I've got a large enough source file that I'd prefer to skip loading a row into my type if the data doesn't meet my requirements. I see that the way to do that is to use CsvReader.Configuration.ShouldSkipRecord during read. My source file has a header row and I'd prefer to use the header name rather than the index of the column. Example Linqpad snippet:

void Main()
{
    CsvReader reader = new CsvReader(new StringReader(CSVSource));
    reader.Configuration.HasHeaderRecord=true;
    reader.Configuration.RegisterClassMap<FlavorFavMap>();
    reader.Configuration.ShouldSkipRecord = row => row[0]=="NY";
    //I'd rather do this
    //reader.Configuration.ShouldSkipRecord = row => row["State"]=="NY"; 
    List<FlavorFav> readData = reader.GetRecords<FlavorFav>().ToList();

    readData.Dump("Parsed Data");
}
public class FlavorFavMap : ClassMap<FlavorFav>
{
    public FlavorFavMap()
    {
        Map(m => m.State).Name("State");
        Map(m => m.Flavor).Name("Flavor");
        Map(m => m.Percent).Name("Percent");
    }
}
public class FlavorFav
{
    public string State { get; set; }
    public string Flavor { get; set; }
    public int Percent { get; set; }
}
public string CSVSource = @"State,Flavor,Percent
NY,Chocolate,60
NY,Vanilla,37
NY,Strawberry,3
AZ,Chocolate,40
AZ,Vanilla,55
AZ,Strawberry,5";

feature

Most helpful comment

I should probably pass in IReaderRow. That would allow you to do row.GetField<T>( string name ).

All 2 comments

I should probably pass in IReaderRow. That would allow you to do row.GetField<T>( string name ).

Yes please, I'd like to somehow look at the raw text line and use it to decide if the row should be used or not.
Today I have to go row by row until the data is found by evaluating Context.RawRecord, but that does not allow me to use IEnumerable that I need for bulk DB inserts.
If the callback includes raw text and whatever was parsed in various formats, that would be ideal.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CallMeBruce picture CallMeBruce  路  4Comments

DmitryEfimenko picture DmitryEfimenko  路  3Comments

Wagimo picture Wagimo  路  4Comments

GraceYuJuSong picture GraceYuJuSong  路  4Comments

SuperSkippy picture SuperSkippy  路  5Comments