Skip to content

Can CsvHelper store the CSV row number in the mapped object?Β #845

@reloaded

Description

@reloaded

Hello!

I'm mapping my CSV records to a concrete object and I was wondering if there's a way to persist the row number in the concrete object?

I have a mapped object that looks like this:

public class CsvRowDto {
  public string FirstName {get; set;}
  public string LastName {get; set;}
}

And I'm trying to achieve something like this:

public class CsvRowDto {
  public int RowNumber {get; set;}
  public string FirstName {get; set;}
  public string LastName {get; set;}
}

The use case for this is to provide better error reporting. When my application detects a validation/business rule error I'd like to report the row number that had an error.

var records = csvReader.GetRecords<CsvRowDto>().ToList();

foreach(var person in records) {
  if(string.IsNullOrEmpty(person.FirstName)) {
    Console.WriteLine("First name is invalid: " + person.RowNumber);
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions