Skip to content

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