简体   繁体   English

当映射到字符串属性时,Csv Helper Fluent类映射会向int字段添加前导零

[英]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.

相关问题 映射类中的未映射属性(Fluent NHibernate 1.1) - Unmapped property in a mapped class (Fluent NHibernate 1.1) 当映射的字段移动到基类时,NHibernate JOIN 映射失败 - NHibernate JOIN mapping fails when the mapped field is moved to a base class 流利的nhibernate:当我需要字符串时,复合键中的枚举被映射为int - Fluent nhibernate: Enum in composite key gets mapped to int when I need string NHibernate :(流畅)基于访问已映射集合属性的Getter的映射/查询 - NHibernate: (Fluent) Mapping / Querying based upon a Getter that accesses an already mapped collection property 使用 CSV 助手进行错误映射,其中一个字段的值为 null - Error mapping with CSV helper the value of one field is null 检测字符串中的前导零 - Detect Leading Zero in String 如何映射字典类型的属性 <string, MyDto> 使用流利的映射 - How to map a property of type Dictionary<string, MyDto> using fluent mapping NHibernate Fluent将字符串属性映射到另一个表的列 - NHibernate Fluent Mapping a String Property to another Table's column IList的流畅NHibernate映射 <int> ? - Fluent NHibernate mapping for IList<int>? 流利的NHibernate:插入或更新时忽略映射属性“ Length” - Fluent NHibernate: mapping property “Length” ignored when inserting or updating
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM