You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can CsvHelper store the CSV row number in the mapped object? #845
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);
}
}
Reacted by Daniel Andraszewski, John Lanz and Florian
I think CSV row number?... I'm not certain on the differences between the two. The text file row number includes blank lines, right?
Let's say that you are a end user of my application and you upload a CSV. When my system detects an error it needs to tell you which row you should take a look at in the CSV file to find and fix the issue....
Reacted by Dave Transom, Johann Dirry, Daniel Andraszewski, Alexey Yakovlev, André Juck Faustino Pereira, Baljeet Singh, Mark, Francis Gauthier and Tomas Karban
Hi,
If the content of records span across multiples rows, the actual row number in the document is returned instead of record number, how do we get record number in csv in such a case
Thank you!
Is this still an option? It looks like the API has changed, and ConvertUsing is no longer available. I have also tried to use a TypeConverter, but haven't found a way to get the row number.
EDIT: Just found the following, which appears to be what I need (untested):
Map(m => m.LineNumber).Convert(row => row.Context.Parser.RawRow);
Activity
JoshClose commentedon Nov 15, 2017
Text file row number or CSV row number? Meaning, a CSV field may have a
\r\nin it.reloaded commentedon Nov 15, 2017
That's a quick response! :)
I think CSV row number?... I'm not certain on the differences between the two. The text file row number includes blank lines, right?
Let's say that you are a end user of my application and you upload a CSV. When my system detects an error it needs to tell you which row you should take a look at in the CSV file to find and fix the issue....
So... I think I need the CSV row number?
JoshClose commentedon Nov 15, 2017
You'll want the file's line number then. A single CSV row may span many lines because a field could contain a '\r\n'.
JoshClose commentedon Nov 15, 2017
Also, when an exception occurs, that information is available in the exception too.
reloaded commentedon Nov 16, 2017
Wonderful! @JoshClose That solved my question. Using
Map(m => m.RowNumber).ConvertUsing(row => row.Context.RawRow);works like a charm.itscodeminer commentedon Oct 18, 2019
Hi,
If the content of records span across multiples rows, the actual row number in the document is returned instead of record number, how do we get record number in csv in such a case
Thank you!
JoshClose commentedon Oct 18, 2019
Use
Rowinstead ofRawRow.capinredbeard commentedon Jan 28, 2021
Is this still an option? It looks like the API has changed, and ConvertUsing is no longer available. I have also tried to use a TypeConverter, but haven't found a way to get the row number.
EDIT: Just found the following, which appears to be what I need (untested):
Map(m => m.LineNumber).Convert(row => row.Context.Parser.RawRow);
JoshClose commentedon Jan 29, 2021
Yes,
ConvertUsingwas renamed toConvert.jzabroski commentedon Feb 3, 2021
@JoshClose I think you should convert this to a Discussion and select the answer so that you can update the answer as you progress CsvHelper.
e.g.,
v12:
v13 - v20:
v21: