c# - 处理映射器属性中的空值
问题描述
我正在尝试读取一个平面文件并执行一些过程。为此,我定义了一个映射器。该映射器将为每个属性分配值。在文档中,日期将用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");
}
}
解决方案
根据 NullFormatter 的定义,(在此处找到),您只能分配 1 个固定值。“如果是固定值,可以使用 NullFormatter.ForValue 方法。”
NullFormatter = NullFormatter.ForValue("NULL")
如果您使用“000000”,那么它应该将 000000 转换为 null,否则,空格将被视为实际值。任意数量的 0 != 6 也将产生非空值。
另外,请定义“但没有用”的意思。请提供详细信息和错误以进行详细说明
推荐阅读
- elasticsearch - elasticsearch 查询是如何在操作系统级别执行的?
- visual-studio-code - VSCode:选择整个函数定义?
- python - 如果缺少某些日子,如何使用 groupby 在 pandas 数据框上创建 30 天的完整窗口
- gitlab-ci - 合并后Gitlab主分支管道未运行
- ios - 如何从安装在 iOS 设备上的 iOS 应用程序中捕获打印语句?
- typescript - 导出可变值:import vs require
- python - 读取 JSON URL 后如何弹出 [List]?
- javascript - Chart.js 3.x 迁移问题 - 首次加载时图表周围出现细白边框/填充
- python-3.x - Pandas Python中具有多个条件的索引和匹配
- node.js - 浏览器客户端和本地安装的服务器守护程序之间的 Auth0