[英]Csv Helper Fluent Class Mapping adds a leading zero to int field when mapped to string property
I have a string property called TimeClockCode. 我有一个名为TimeClockCode的字符串属性。 Which is mapped to index(0) of the csv file. 哪个映射到csv文件的索引(0)。 Index(0) contains integers such as 11565. But after mapping to string TimeClockCode, Index(0)包含整数,例如11565.但是在映射到字符串TimeClockCode之后,
Like this: Map(p => p.TimeclockCode).Index(0);
像这样: Map(p => p.TimeclockCode).Index(0);
or like this: Map(p => p.TimeclockCode).ConvertUsing(row => row.GetField<string>(0));
或者像这样: Map(p => p.TimeclockCode).ConvertUsing(row => row.GetField<string>(0));
both result in string 011565. 两者都导致字符串011565。
I solved this by reading it as an int and calling toString on it. 我通过将其作为int读取并在其上调用toString来解决这个问题。
Map(p => p.TimeclockCode).ConvertUsing(row => row.GetField<int>(0).ToString());
Which gave me desired results of 11565. 这给了我11565的预期结果。
Am I missing something about the implementation of fluent mappings? 我是否遗漏了一些关于流畅映射的实现? Where is the leading zero coming from? 领先的零来自哪里?
I couldn't replicate your problem. 我无法复制你的问题。 I used this code: 我用过这段代码:
static void Main(string[] args)
{
var s = "\"11565\",\"a\",\"012\",\"4.5\",\"01\",\"555\"\r\n";
using (var sr = new StringReader(s))
{
var csv = new CsvReader(sr);
csv.Configuration.HasHeaderRecord = false;
csv.Configuration.RegisterClassMap<MyClassMap>();
while (csv.Read())
{
var record = csv.GetRecord<MyClass>();
}
}
}
public class MyClass
{
public string Test { get; set; }
public string TimeclockCode { get; set; }
}
public sealed class MyClassMap : CsvClassMap<MyClass>
{
public MyClassMap()
{
Map(p => p.TimeclockCode).Index(0);
Map(p => p.Test).Index(2);
}
}
I can only assume that the value in the CSV is 011565 or that you have some other issue that is not evident from the text of the question. 我只能假设CSV中的值是011565,或者您还有一些其他问题,这些问题在问题文本中并不明显。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.