如何使用 FileHelpers 返回完全填充的对象以获得部分完整的数据?

发布于 2024-09-26 22:57:01 字数 2107 浏览 5 评论 0原文

假设您有一个具有以下结构的 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 行相同的 IDDescription。每行的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女 2024-10-03 22:57:01

您可以尝试使用 AfterRead 接口而不是这样的事件。

如果我理解正确,你需要做类似的事情:

[DelimitedRecord(",") ]
[IgnoreFirst(1)] 
public class Product
:INotifyRead
{
    public int? ID;
    public string Description;

    [FieldConverter(ConverterKind.Decimal)]
    public decimal? Val;

    private static Product Previuous;

    public void AfterRead(EngineBase engine, string line)
    {
        if (!ID.HasValue && Previous != null)
              this.ID = Previus.ID;

        if (!Val.HasValue && Previous != null)
              this.Val= Previus.Val;

        Previuous = this;
    }
}

希望这有帮助

You can try using the interfaces for AfterRead instead of events like this.

If I understand right you need to do something like that:

[DelimitedRecord(",") ]
[IgnoreFirst(1)] 
public class Product
:INotifyRead
{
    public int? ID;
    public string Description;

    [FieldConverter(ConverterKind.Decimal)]
    public decimal? Val;

    private static Product Previuous;

    public void AfterRead(EngineBase engine, string line)
    {
        if (!ID.HasValue && Previous != null)
              this.ID = Previus.ID;

        if (!Val.HasValue && Previous != null)
              this.Val= Previus.Val;

        Previuous = this;
    }
}

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文