如何使用 FileHelpers 返回完全填充的对象以获得部分完整的数据?
假设您有一个具有以下结构的 CSV 文件
LINE1: ID,Description,Value
LINE2: 1,Product1,2
LINE3: ,,3
LINE4: ,,4
LINE5: 2,Product2,2
LINE6: ,,3
LINE7: ,,5
具有相应的 FileHelpers
定义类
[DelimitedRecord(",") ]
[IgnoreFirst(1)]
public class Product
{
public int ID { get; set; }
public string Description { get; set; }
[FieldConverter(ConverterKind.Decimal)]
public decimal Val{ get; set; }
}
如何使用 FileHelpers< /a>,返回对象以使每个对象都完全填充?
让我澄清一下,目前,使用 engine.ReadFile
的结果只会生成第 2 行和第 5 行完全填充的对象,这是有道理的,因为字段是空白的。但是,第 3 行和第 4 行中的对象也应具有与第 1 行相同的 ID
和 Description
。每行的 Value
字段应保持不变。我不确定数据的正确术语是什么,但它似乎“部分标准化”。
我当前解决此问题的方法如下,使用 AfterRead 事件。我对事件、事件处理程序、委托等没有很好的掌握,所以我正在寻找一种使用 FileHelpers 来实现这一点的替代方法。它看起来也很笨重。
// call the csv repo using the ffg lines within a console app
var repo2 = new CSVRepositoryBase<Product>();
repo2.AfterRead +=new AfterReadHandler<object>(repo2_AfterRead);
var products = repo2.Read("some file path");
public static Product PreviousRecord { get; set; }
private static void repo2_AfterRead
(EngineBase engine, FileHelpers.Events.AfterReadEventArgs<object> e)
{
var record = (Product)e.Record;
if (PreviousRecord == null)
{
PreviousRecord = new Product();
PreviousRecord.ID = record.ID;
}
if (String.IsNullOrEmpty(record.ID)) // newline record = blank row
{
record.ID = PreviousRecord.ID;
}
PreviousRecord.ID = record.ID;
}
public class CSVRepositoryBase<T> where T : class
{
// shorten for brevity
public IEnumerable<T> Read(string fileName){/*snip*/ }
#region Events
public event AfterReadHandler<object> AfterRead
{
add { engine.AfterReadRecord += value; }
remove { engine.AfterReadRecord -= value; }
}
#endregion
}
Assume you have a CSV file with the following structure
LINE1: ID,Description,Value
LINE2: 1,Product1,2
LINE3: ,,3
LINE4: ,,4
LINE5: 2,Product2,2
LINE6: ,,3
LINE7: ,,5
With the corresponding FileHelpers
definition class
[DelimitedRecord(",") ]
[IgnoreFirst(1)]
public class Product
{
public int ID { get; set; }
public string Description { get; set; }
[FieldConverter(ConverterKind.Decimal)]
public decimal Val{ get; set; }
}
How do use FileHelpers, to return objects such that each object is fully populated?
Let me clarify, currently, result of using engine.ReadFile
will only produce objects from Lines 2 and 5 as fully populated, which makes sense since the fields are blank. However, objects from Lines 3 and 4 should also have the same ID
and Description
as Line 1. The Value
field should remain untouched for each line. I am not sure what the correct term for the data is, but it seems to 'partially normalised'.
My current approach to solve this is as follows using the AfterRead
event. I do not have a good grasp of Events, Event Handlers, delegates etc, so I am looking for an alternative way to achieve this using FileHelpers. It also seems very clunky.
// call the csv repo using the ffg lines within a console app
var repo2 = new CSVRepositoryBase<Product>();
repo2.AfterRead +=new AfterReadHandler<object>(repo2_AfterRead);
var products = repo2.Read("some file path");
public static Product PreviousRecord { get; set; }
private static void repo2_AfterRead
(EngineBase engine, FileHelpers.Events.AfterReadEventArgs<object> e)
{
var record = (Product)e.Record;
if (PreviousRecord == null)
{
PreviousRecord = new Product();
PreviousRecord.ID = record.ID;
}
if (String.IsNullOrEmpty(record.ID)) // newline record = blank row
{
record.ID = PreviousRecord.ID;
}
PreviousRecord.ID = record.ID;
}
public class CSVRepositoryBase<T> where T : class
{
// shorten for brevity
public IEnumerable<T> Read(string fileName){/*snip*/ }
#region Events
public event AfterReadHandler<object> AfterRead
{
add { engine.AfterReadRecord += value; }
remove { engine.AfterReadRecord -= value; }
}
#endregion
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用 AfterRead 接口而不是这样的事件。
如果我理解正确,你需要做类似的事情:
希望这有帮助
You can try using the interfaces for AfterRead instead of events like this.
If I understand right you need to do something like that:
Hope this helps