C# CsvHelper:索引超出了数组的边界
我的地图课是C# CsvHelper:索引超出了数组的边界,c#,csvhelper,C#,Csvhelper,我的地图课是 public sealed class ProductMap : CsvClassMap<Product> { public ProductMap() { Map(m => m.VatRate).ConvertUsing(row => { if (string.IsNullOrEmpty(row.GetField<string>
public sealed class ProductMap : CsvClassMap<Product>
{
public ProductMap()
{
Map(m => m.VatRate).ConvertUsing(row =>
{
if (string.IsNullOrEmpty(row.GetField<string>("VatRate"))) throw new Exception("VatRate is required.");
return row.GetField<string>("VatRate");
}).Index(0);
}
}
在试图获取列表中的记录时
IEnumerable<Product> products = csv.GetRecords<Product>().ToList();
IEnumerable products=csv.GetRecords().ToList();
我得到以下错误
索引超出了数组的边界
这是StackTrace
在中的BackFinal.Parse.b__1(异常ex,ICsvReader行)
d:\Projects\BackFinal\CSVImport.cs:at的第80行
CsvHelper.CsvReader.d_u631.MoveNext()在
C:\Projects\CsvHelper\src\CsvHelper\CsvReader.cs:944行
System.Collections.Generic.List
1..ctor(IEnumerable1 collection)
在System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source)处
BackFinal.Parse..ctor(上传文件结果R)在
d:\Projects\BackFinal\CSVImport.cs:第87行
如果我将Product
类中VatRate
的类型从decimal
更改为string
,我不会得到任何错误
能给我一个解决方案吗?你能发布一份你的数据样本吗?也许这与分隔符有关,分隔符用于分隔十进制数字和CSV行中的单个项目。在ProductMap中,在您进行VatRate之前是or吗?
IEnumerable<Product> products = csv.GetRecords<Product>().ToList();