dis*_*r-5 2 c# automapper csvhelper
如果CSV文件中缺少某个字段,则会引发异常.当字段丢失时,我宁愿映射另一个值(例如空字符串).
Map(dest => dest.PatientID).Name("Patient ID");
CsvHelper.CsvMissingFieldException: 'Fields 'Patient ID' do not exist in the CSV file.'
Run Code Online (Sandbox Code Playgroud)
如果使用配置设置IgnoreReadingExceptions,则不会将任何记录读入结果.
var csv = new CsvReader(sr);
csv.Configuration.RegisterClassMap<TMap>();
csv.Configuration.IgnoreReadingExceptions = true;
records = csv.GetRecords<TR>().ToList();
Run Code Online (Sandbox Code Playgroud)
如何更改映射,以便在映射字段为MISSING时,可以将其替换为另一个表达式?
Jos*_*ose 13
在2.x中你可以这样做:
csv.Configuration.WillThrowOnMissingField = false;
Run Code Online (Sandbox Code Playgroud)
在3.x中你可以这样做:
// Turn off.
csv.Configuration.MissingFieldFound = null;
// Log missing field.
csv.Configuration.MissingFieldFound = ( headerNames, index, context ) =>
{
logger.WriteLine( $"Field with names ['{string.Join( "', '", headerNames )}'] at index '{index}' was not found. );
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4652 次 |
| 最近记录: |