如何为FileHelpers元素类的字段定义默认值 [英] How to define a default value for a field of a FileHelpers element class

查看:121
本文介绍了如何为FileHelpers元素类的字段定义默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加载一个CSV文件(delims是';'和引号')

I'm trying to load a CSV file (delims are ';' and quotes are '"').

我已经成功地创造了一切(向导工具是真棒),但有,我不能找到一个解决方案的一件事。

I have successfully created everything (the wizard tool is awesome), but there's one thing that I can't find a solution for.

基本上,我有一个整数(System.Int32的)列。理论上的的记录将有一个的正整数的该列。但有时,我可能会遇到在该列中的值N / A。我想实现什么,是有FileHelpers指定一个默认值(-1工作得很好),遇到的时候N / A在该列。

Basically, I have an integer (System.Int32) column. In theory most of the records will have a positive integer value in that column. Sometimes, however, I might encounter a value "N/A" in that column. What I want to achieve, is have FileHelpers assign a default value (-1 works just fine), when it encounters "N/A" in that column.

有谁知道这是可能的吗?

Does anyone know if this is possible?

PS:我可能需要一个System.DateTime的领域做同样的(它可能也有N / A有时)

PS: I might need to do the same for a System.DateTime field (it might also have "N/A" sometimes).

推荐答案

想到的,做你想做的是使用了 FieldConverter 属性的最佳方法这些领域并创建指定所需的字段有默认值自定义转换器类。

The best way that comes to mind to do what you want would be to use the FieldConverter attribute on those fields and create custom converter classes that assign the default value you want the field to have.

您需要创建一个类继承 ConverterBase ,然后提供实现两个虚拟方法, StringToField() FieldToString()

You need to create a class that inherits ConverterBase, and then provide implementations for the two virtual methods, StringToField() and FieldToString().

FieldToString()方法,你会检查字符串是否等于N / A。如果是,返回所需的默认值。

In the FieldToString() method, you'll check to see if the string is equal to "N/A". If it is, return the default value you want.

您需要两个不同的类,一个可以处理的Int32 和一个能够处理的DateTime

You will need two different classes, one that can handle Int32 and one that can handle DateTime.

该FileHelpers文档有如何做到这一点的一个例子。 链接

The FileHelpers documentation has an example of how to do this. Link

这篇关于如何为FileHelpers元素类的字段定义默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆