From 5e2f21539c0a1e2dc0380e5ac92aa65b7d41db01 Mon Sep 17 00:00:00 2001 From: Daniil Tolstov Date: Wed, 20 Mar 2024 18:12:26 +0100 Subject: [PATCH] Handle scenario when record has less values than header (cherry picked from commit 8ed502933e3a645d6f4edfcf7668ffadc909eb20) --- CsvHelper.FastDynamic/CsvReaderExtensions.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/CsvHelper.FastDynamic/CsvReaderExtensions.cs b/CsvHelper.FastDynamic/CsvReaderExtensions.cs index 2aeea5e..2067fa1 100644 --- a/CsvHelper.FastDynamic/CsvReaderExtensions.cs +++ b/CsvHelper.FastDynamic/CsvReaderExtensions.cs @@ -40,7 +40,14 @@ public static IEnumerable EnumerateDynamicRecords(this CsvReader csvRea for (var i = 0; i < csvHeader.FieldNames.Length; i++) { - values[i] = csvReader.Parser[i]; + if (i >= csvReader.Parser.Count) + { + values[i] = default; + } + else + { + values[i] = csvReader.Parser[i]; + } } record = new CsvRecord(csvHeader, values); @@ -105,7 +112,14 @@ public static async IAsyncEnumerable EnumerateDynamicRecordsAsync(this for (var i = 0; i < csvHeader.FieldNames.Length; i++) { - values[i] = csvReader.Parser[i]; + if (i >= csvReader.Parser.Count) + { + values[i] = default; + } + else + { + values[i] = csvReader.Parser[i]; + } } record = new CsvRecord(csvHeader, values);