将 DataTable 从平面表解析为标准化表

发布于 2024-11-02 16:04:49 字数 718 浏览 1 评论 0原文

我有 B2B 集成的需求,我将从数据集中读取平面表并解析为数据表的规范化形式。

我将在数据表中重新获取列 示例数据

Invoice num     Amount    LineNum   Line Amout  Ledger
INV1            100       1         50          11101
INV1            100       2         50          25631 

行将与不同的发票重复

如何可以清楚地选择到新的数据表中?使用 ADO.NET

我想将数据解析为以下格式

标题表

  Invoice num     Amount    
  INV1            100         

行表

  Invoice num  LineNum   Line Amout  Ledger
  INV1          1         50          11101
  INV1          2         50          25631 

问题:我不知道采用上述格式的最佳方法是什么?我看到使用 linq 、 DataTable 、 Views 的示例?我正在寻找代码片段。

I have a requirement for B2B integration, I will reading flat table from dataset and parse to a normalize form of datatable.

I will have reapting colums in the datatable
Sample Data

Invoice num     Amount    LineNum   Line Amout  Ledger
INV1            100       1         50          11101
INV1            100       2         50          25631 

rows will repeat with different invoices

How can distinctly select into new datatable ? using ADO.NET

I want to parse the data into following format

Header Table

  Invoice num     Amount    
  INV1            100         

Line Table

  Invoice num  LineNum   Line Amout  Ledger
  INV1          1         50          11101
  INV1          2         50          25631 

QUESTION : I dont know what would be the best way to bring the above format ? I see examples usign linq , DataTable, Views ? I looking for a code snippet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

森末i 2024-11-09 16:04:49

好的,为了开始解决这个问题,我正在使用 DataTable 和定义如下的数据。更改名称和类型以满足您的需要。

// I am building this table in code just for the purposes of this answer.
// If you already have your data table, ignore!
DataTable salesTable = new DataTable();
salesTable.Columns.Add("InvoiceNum", typeof(string));
salesTable.Columns.Add("Amount", typeof(decimal));
salesTable.Columns.Add("LineNum", typeof(int));
salesTable.Columns.Add("LineAmount", typeof(decimal));
salesTable.Columns.Add("Ledger", typeof(string));

// This is also just to populate data for the sample.
// Omit as you already have your data.
salesTable.Rows.Add("INV1", 100M, 1, 50M, "11101");
salesTable.Rows.Add("INV1", 100M, 1, 50M, "25631");

请注意,我使用的是接受 params object[] 数组的 .Rows.Add 重载。我传入的值按照它们应填充的列的顺序和类型排列。下面的代码使用相同的方法。

我要做的第一件事是为新的标准化格式定义表格。首先,表头。

DataTable headerTable = new DataTable();
headerTable.Columns.Add("InvoiceNum", typeof(string));
headerTable.Columns.Add("Amount", typeof(decimal));

然后是行项目表。

DataTable lineTable = new DataTable();
lineTable.Columns.Add("InvoiceNum", typeof(string));
lineTable.Columns.Add("LineNum", typeof(int));
lineTable.Columns.Add("LineAmount", typeof(decimal));
lineTable.Columns.Add("Ledger", typeof(string));

之后,我将使用 LINQ 根据发票编号对原始销售表进行分组。

var groupedData = from row in salesTable.AsEnumerable()
                  group row by row.Field<string>("InvoiceNum") into grp
                  select grp;

之后,只需迭代组并将数据添加到新表即可。

foreach (var invoiceGroup in groupedData)
{
    string invoiceNumber = invoiceGroup.Key;
    decimal amount = invoiceGroup.First().Field<decimal>("Amount");

    headerTable.Rows.Add(invoiceNumber, amount);

    foreach (DataRow row in invoiceGroup)
    {
        lineTable.Rows.Add(
                invoiceNumber,
                row.Field<int>("LineNum"),
                row.Field<decimal>("LineAmount"),
                row.Field<string>("Ledger")
            );
    }
}

现在您的数据已采用您喜欢的标准化格式。再次更改相关的列名称和数据类型以满足您的需求。

OK, to start the problem, I am working with a DataTable and data defined as follows. Change names and types to suit your needs.

// I am building this table in code just for the purposes of this answer.
// If you already have your data table, ignore!
DataTable salesTable = new DataTable();
salesTable.Columns.Add("InvoiceNum", typeof(string));
salesTable.Columns.Add("Amount", typeof(decimal));
salesTable.Columns.Add("LineNum", typeof(int));
salesTable.Columns.Add("LineAmount", typeof(decimal));
salesTable.Columns.Add("Ledger", typeof(string));

// This is also just to populate data for the sample.
// Omit as you already have your data.
salesTable.Rows.Add("INV1", 100M, 1, 50M, "11101");
salesTable.Rows.Add("INV1", 100M, 1, 50M, "25631");

Notice that I'm using the overload of .Rows.Add that accepts a params object[] array. The values I'm passing in are in the order and type of the columns they should populate. The code below uses the same approach.

First thing I want to do is define the tables for your new normalized format. First, the header table.

DataTable headerTable = new DataTable();
headerTable.Columns.Add("InvoiceNum", typeof(string));
headerTable.Columns.Add("Amount", typeof(decimal));

And then the line item table.

DataTable lineTable = new DataTable();
lineTable.Columns.Add("InvoiceNum", typeof(string));
lineTable.Columns.Add("LineNum", typeof(int));
lineTable.Columns.Add("LineAmount", typeof(decimal));
lineTable.Columns.Add("Ledger", typeof(string));

After this, I'm going to utilize LINQ to group the original sales table based on the invoice number.

var groupedData = from row in salesTable.AsEnumerable()
                  group row by row.Field<string>("InvoiceNum") into grp
                  select grp;

After this, it's just a matter of iterating over the groups and adding the data to the new tables.

foreach (var invoiceGroup in groupedData)
{
    string invoiceNumber = invoiceGroup.Key;
    decimal amount = invoiceGroup.First().Field<decimal>("Amount");

    headerTable.Rows.Add(invoiceNumber, amount);

    foreach (DataRow row in invoiceGroup)
    {
        lineTable.Rows.Add(
                invoiceNumber,
                row.Field<int>("LineNum"),
                row.Field<decimal>("LineAmount"),
                row.Field<string>("Ledger")
            );
    }
}

And now you have your data in the normalized format you prefer. Again, change relevant column names and data types to suit your needs.

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