-
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:
And I'm trying to achieve something like this:
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.
|
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 13 replies
-
Text file row number or CSV row number? Meaning, a CSV field may have a |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
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'. void Main()
{
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
using (var reader = new StreamReader(stream))
using (var csv = new CsvReader(reader))
{
writer.WriteLine("Id,Name");
writer.WriteLine("1,one");
writer.WriteLine("2,two");
writer.Flush();
stream.Position = 0;
csv.Configuration.RegisterClassMap<TestMap>();
csv.GetRecords<Test>().ToList().Dump();
}
}
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public int RowNumber { get; set; }
}
public sealed class TestMap : ClassMap<Test>
{
public TestMap()
{
Map(m => m.Id);
Map(m => m.Name);
Map(m => m.RowNumber).ConvertUsing(row => row.Context.RawRow);
}
} |
Beta Was this translation helpful? Give feedback.
-
Also, when an exception occurs, that information is available in the exception too. |
Beta Was this translation helpful? Give feedback.
-
Wonderful! @JoshClose That solved my question. Using |
Beta Was this translation helpful? Give feedback.
-
Hi, |
Beta Was this translation helpful? Give feedback.
-
Use |
Beta Was this translation helpful? Give feedback.
-
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): |
Beta Was this translation helpful? Give feedback.
-
Yes, |
Beta Was this translation helpful? Give feedback.
-
@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., v23 (current): Map(m => m.LineNumber).Convert(record => record.Row.Parser.Row); v21: Map(m => m.LineNumber).Convert(row => row.Context.Row); v13 - v20: Map(m => m.LineNumber).ConvertUsing(row => row.Context.Row); v12: Map(m => m.LineNumber).ConvertUsing(row => row.Context.RawRow); |
Beta Was this translation helpful? Give feedback.
-
So it is not clear. Can I get the line number from the input document based on using attributes? I am not using a map. I appreciate error logging includes this info but sometime I show my own errors. |
Beta Was this translation helpful? Give feedback.
@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.,
v23 (current):
v21:
v13 - v20:
v12: