简体   繁体   English

如何使用FileHelpers或C#中的其他库从单个文本文件读取两个实体

[英]How to read two entities from single text file using filehelpers or other library in C#

Text file contains order and order detail lines. 文本文件包含订单和订单明细行。 First line in order header. 订单标题中的第一行。 After that there is variable number of detail lines. 之后,明细行数是可变的。 After detail there is blank line followed by next order etc. For order line first field is order number. 详细信息之后是空白行,然后是下一个订单等。对于订单行,第一个字段是订单号。 For detail line first field is 1 always. 对于明细行,第一字段始终为1。

128502 02.01.2012 20120 02.01.2012
1   Wine    0   1300
1   Meat    5,8333  5,83

128503 02.01.2012 20123 02.01.2012  
1   Wine    20  130
1   Meat    1,33    283,23
1   Cow 2,333   333,23

....

This file need to be readed into list of entities: 该文件需要读入实体列表:

class Order {
  public string Number; // order number from first field, primary key 
  public string Date;
  ... other fields  
}


class OrderDetails {
  public string Number; // order number from previous line , foreign key to Order
  public string ProductName;
  ... other fields  
}

(Instead on Number custom integer id column can also used for relation) (代替Number自定义整数id列也可用于关联)

How to read such file in C# ASP.NET MVC2 using FileHelpers library or other way ? 如何使用FileHelpers库或其他方式在C#ASP.NET MVC2中读取此类文件?

Update 更新资料

sample 样品

http://www.filehelpers.com/example_multirecords.html http://www.filehelpers.com/example_multirecords.html

referenced from 引用自

Multiple CSV strutures with FileHelpers 使用FileHelpers的多个CSV结构

shows how to read two tables. 显示了如何读取两个表。 How to create relation between those tables: During reading foreign key to order column should added to details table. 如何在这些表之间创建关系:在读取外键到订单列时,应将其添加到明细表中。 How to implement this, getting last order number from previous order line and annd it to detail record ? 如何实现这一点,从上一个订单行获取最后一个订单号,并将其添加到明细记录中?

Well you could go brute force all in one go. 好吧,你可以一口气去蛮力。 Read in the entire file Split on the blank like. 读入整个文件,像空白一样拆分。 Create an Order out of the first line of each split Create details out of the rest. 在每个拆分的第一行之外创建订单在其余部分之外创建明细。

If the files are huge then you could read in up to each blank line and then process as above. 如果文件很大,则可以读入每个空白行,然后按上述步骤进行处理。

Another solution that might have some advantages. 另一个可能具有某些优点的解决方案。

Run the file through a process that edit's the detail line and adds in the order number from it's realted order line. 通过编辑明细行并从其实际订单行添加订单号的过程来运行文件。 Then another that splits out order and detail into two separate files. 然后是另一个将顺序和详细信息分成两个单独文件的文件。 Then process orders then details without having to worry about all this dittoing and two structures. 然后处理订单,然后处理细节,而不必担心所有这些同等和两个结构。

Fourth solution, is give whoever came up with this format a good beating to encourage them to do it properly... 第四个解决方案是,给想出这种格式的人一个很好的殴打,以鼓励他们正确地使用它。

There's no magic wand solution for this, the file format is simply wholly unsuitable for any efficent passing of data to something that might store it sensibly. 没有万能的解决方案,文件格式完全不适合将数据有效地传递到可能明智地存储的文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReadStreamAsDT-Filehelpers和C#-如何使用FileHelpers动态读取CSV? - ReadStreamAsDT - Filehelpers and C# - How do I dynamically read in a CSV using filehelpers? 如何使用 C#/FileHelpers ExcelNPOIStorage 从 Excel 文件中提取数据 - How to extract data from an Excel file with C# / FileHelpers ExcelNPOIStorage 如何使用FileHelpers库从csv中只读取一定数量的字段? - How do I read only a set number of fields from a csv using the FileHelpers library? c#如何使用普通文本元素和xml文本元素读取单个文件 - c# How to read a single file with normal and xml text elements 如何在c#中从文件中读取文本从一个值到另一个值 - How to read text from file from one value to other in c# C# 使用 FileHelpers 将 csv 文件导入 SQL 数据库 - C# importing csv file into SQL database using FileHelpers 如何使用LINQ或C#以其他方式匹配来自两个数据表的数据具有单行 - How to match data from two data tables have single row using LINQ or C# other way 如何使用linq to sql c将两个不同的实体结果集分配到单个var变量中# - How to assign two different entities result set into single var variable using linq to sql c# 如何使用C#在SharePoint Online中读取单个文件的内容 - How to read the contents of a single file in SharePoint Online using C# C# .NET5 单个文件应用程序从目录读取和解析 Json / Txt 或其他文件格式 - C# .NET5 Single File Application Read and Parse Json / Txt or other file formats from directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM