将可检测的 Nullable 值添加到 CsvHelper [英] Adding detectable Nullable values to CsvHelper
问题描述
我想知道 Josh Close 的 CsvHelper 在配置中是否有任何我缺少的将值转换为 null 的内容.我是这个库的忠实粉丝,但我一直认为应该有某种配置让它知道你的文件中哪些值代表 NULL.一个例子是一个值为NA"、EMPTY"、NULL"等的列.我确信我可以创建自己的 TypeConverter,但我希望有一个更简单的选项可以在配置中的某处设置为这在我遇到的文件中很常见.
I was wondering if CsvHelper by Josh Close has anything in the configuration I am missing to translate values to null. I am a huge fan of this library, but I always thought there should be some sort of configuration to let it know what values represent NULL in your file. An example would be a column with the value "NA", "EMPTY", "NULL", etc. I am sure I could create my own TypeConverter, but I was hoping there would be an easier option to set somewhere in a config as this tends to be fairly common with files I encounter.
是否有配置设置可以相对轻松地执行此操作?
Is there a configuration setting to do this relatively easily?
我在 CsvHelper.TypeConversion 命名空间中找到了 TypeConversion,但不确定在哪里应用类似这样的内容或正确用法的示例:
I found the TypeConversion in the CsvHelper.TypeConversion namespace but am not sure where to apply something like this or an example of the correct usage:
new NullableConverter(typeof(string)).ConvertFromString(new TypeConverterOptions(), "NA")
我也在使用最新版本 2.2.2
I am also using the latest version 2.2.2
谢谢!
推荐答案
CsvHelper 绝对可以处理可空类型.如果空白列被视为 null,则不需要滚动自己的 TypeConverter.对于我的示例,我假设您使用的是用户定义的流畅映射.
CsvHelper can absolutely handle nullable types. You do not need to roll your own TypeConverter if a blank column is considered null. For my examples I am assuming you are using user-defined fluent mappings.
您需要做的第一件事是为您的 Nullable 类型构造一个 CsvHelper.TypeConverter
对象.请注意,我将使用 int
因为字符串默认允许空值.
The first thing you need to do is construct a CsvHelper.TypeConverter
object for your Nullable types. Note that I'm going to use int
since strings allow null values by default.
public class MyClassMap : CsvClassMap<MyClass>
{
public override CreateMap()
{
CsvHelper.TypeConversion.NullableConverter intNullableConverter = new CsvHelper.TypeConversion.NullableConverter(typeof(int?));
Map(m => m.number).Index(2).TypeConverter(intNullableConverter);
}
}
接下来是在您的 CsvReader 对象上设置属性以允许空白列 &自动修剪您的字段.我个人喜欢通过在构建我的 CsvReader
对象之前使用我的所有设置创建一个 CsvConfiguration
对象来做到这一点.
Next is setting the attribute on your CsvReader object to allow blank columns & auto-trim your fields. Personally like to do this by creating a CsvConfiguration
object with all of my settings prior to constructing my CsvReader
object.
CsvConfiguration csvConfig = new CsvConfiguration();
csvConfig.RegisterClassMap<MyClassMap>();
csvConfig.WillThrowOnMissingField = false;
csvConfig.TrimFields = true;
然后你可以调用 myReader = new CsvReader(stream, csvConfig)
来构建 CsvReader
对象.
Then you can call myReader = new CsvReader(stream, csvConfig)
to build the CsvReader
object.
如果您需要为 null 定义值,例如 "NA" == null
那么您将需要推出自己的 CsvHelper.TypeConversion
类.我建议您扩展 NullableConverter
类来执行此操作并覆盖构造函数和 ConvertFromString
方法.不过,使用空值作为 null 确实是最好的选择.
IF you need to have defined values for null such as "NA" == null
then you will need to roll your own CsvHelper.TypeConversion
class. I recommend that you extend the NullableConverter
class to do this and override both the constructor and ConvertFromString
method. Using blank values as null is really your best bet though.
这篇关于将可检测的 Nullable 值添加到 CsvHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!