Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle scenario when record has less values than header #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Tolstovku
Copy link

Current implementation of EnumerateDynamicRecords() differs from that of original GetRecords() in case when record has less values than header. That is because only header length is considered in enumeration.

Example 1

CSV:

h1,h2,h3
v1,v2

GetRecords<dynamic>()

Using csvReader.GetRecords<dynamic>() as

    foreach(IDictionary<string, object> record in reader.GetRecords<dynamic>()) // EnumerateDynamicRecords() here for next example
    {
        foreach (var keyValuePair in record)
        {
            Console.WriteLine($"{keyValuePair.Key}: {keyValuePair.Value}");
        }
    }

Result:

h1: v1
h2: v2
h3:

EnumerateDynamicRecords()

Result:

h1: v1
h2: v2
h3: h3

Example 2

CSV:

h1,h2,h3,h4
v1,v2

GetRecords<dynamic>()

Result:

h1: v1
h2: v2
h3:
h4:

EnumerateDynamicRecords()

Result:

Unhandled exception. CsvHelper.ReaderException: An unexpected error occurred.
IReader state:
   ColumnCount: 2
   CurrentIndex: -1
   HeaderRecord:
["h1","h2","h3","h4"]
IParser state:
   ByteCount: 0
   CharCount: 17
   Row: 2
   RawRow: 2
   Count: 2
   RawRecord:
v1,v2

 ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at CsvHelper.CsvParser.GetField(Int32 index)
   at CsvHelper.CsvParser.get_Item(Int32 index)
   at CsvHelper.FastDynamic.CsvReaderExtensions.EnumerateDynamicRecords(CsvReader csvReader)+MoveNext() in ...\CsvReaderExtensions.cs:line 43
   --- End of inner exception stack trace ---
   at CsvHelper.FastDynamic.CsvReaderExtensions.EnumerateDynamicRecords(CsvReader csvReader)+MoveNext() in ...CsvReaderExtensions.cs:line 54

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant