mapper属性中的空值

问题描述 投票:0回答:1

我正在尝试读取平面文件并执行一些处理。为此,我定义了一个映射器。该映射器将为每个属性分配值。在文档中,日期将以“ yyMMdd”格式表示,并且可以将“”或“ 000000”作为空值。这意味着,如果日期为6个零或6个空格,则输出应为null。我试图通过定义NullFormater来做到这一点。但是没有用。

所以任何人都可以让我知道我该怎么做。非常感谢。

我的代码

public class Test : DocumentRecordBase
{
public string StorageOrganisation { get; set; }
public Guid? StorageOrganisationId { get; set; }
public string StorageDescription { get; set; }
public DateTime? PaymentDueDate { get; set; }
public decimal? DiscountRate { get; set; }
public int? MaximumDaysDiscount { get; set; }
public DateTime? DateStorageChargeCommences { get; set; }
public decimal? StorageChargePerBalePerDay { get; set; }
public decimal? PenaltyInterestRate { get; set; }
public DateTime? LotAvailableDate { get; set; }
public decimal? PostSaleRechargeRebate { get; set; }

public Test() : base()
{
}

    public override T GetDocumentRecord<T>()
    {
        if (typeof(T) == typeof(Test))
        {
            return this as T;
        }
        return null;
    }


    public static IFixedLengthTypeMapper<Test> GetMapper()
    {
        var mapper = FixedLengthTypeMapper.Define<Test>();
        mapper.Property(r => r.RecordType, 2);
        mapper.Property(r => r.RecordSubType, 1);

        mapper.Property(r => r.PaymentDueDate, 6)
            .ColumnName("PaymentDueDate")
            .InputFormat("yyMMdd")
            .NullFormatter(NullFormatter.ForValue("000000")); // if the read value is "000000" or "      " then should pass as null


        mapper.CustomMapping(new RecordNumberColumn("RecordNumber")
        {
            IncludeSchema = true,
            IncludeSkippedRecords = true
        }, 0).WithReader(r => r.RecordNumber);
        return mapper;
    }

    public static bool GetMapperPredicate(string x)
    {
        return x.StartsWith("11A");
    }

}
c# automapper flat-file fixed-length-file
1个回答
0
投票

根据NullFormatter(found here)的定义,您只能分配1个固定值。 “如果它是固定值,则可以使用NullFormatter.ForValue方法。”

NullFormatter = NullFormatter.ForValue("NULL") 

如果使用“ 000000”,则应将000000转换为null,否则,空格将被视为实际值。任何0s!= 6也将导致非null值。

[另外,请定义“但没用”的意思。请提供详细信息和错误以进行详细说明

© www.soinside.com 2019 - 2024. All rights reserved.