【问题标题】:Handle null values in mapper property处理映射器属性中的空值
【发布时间】:2019-12-02 04:39:17
【问题描述】:

我正在尝试读取一个平面文件并进行一些处理。为此,我定义了一个映射器。该映射器将为每个属性分配值。在文档中,日期将以yyMMdd 格式表示,并且可以将“”或000000 作为空值。这意味着,如果日期是 6 个零或 6 个空格,则输出应为空。我试图通过定义 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# null automapper flat-file fixed-length-file


    【解决方案1】:

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

    NullFormatter = NullFormatter.ForValue("NULL") 
    

    如果您使用“000000”,那么它应该将 000000 转换为 null,否则,空格将被视为实际值。任意数量的 0 != 6 也会产生非空值。

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

    【讨论】:

    • 您好,非常感谢您的帮助。我参考了上面的链接。但在我的逻辑中,我需要验证这两种空格式化程序类型。不仅是'000000'
    • 如果你不能使用 .NullFormatter 两次,不确定它是否允许多值转换。 .NullFormatter(NullFormatter.ForValue("000000")).NullFormatter(...);
    猜你喜欢
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多