【问题标题】:How to Split datatable into multiple datatable using particular id without using linq in c#?如何在不使用 C# 中的 linq 的情况下使用特定 id 将数据表拆分为多个数据表?
【发布时间】:2015-09-04 10:01:21
【问题描述】:

我想将单个 DataTable 拆分为多个 DataTables。

Table1 包含 clientid、clientname

(1,client1)(1,client2)(1,client3)(2,client4)(2,client5)

我想把这张表拆分成

table2 = (1,client1)(1,client2)(1,client3)

table3 将有 (2,client4)(2,client5)。

相同的客户端 ID DataRows 将移至单独的 DataTable。我该怎么做?

我试过了,但它不起作用。我想在 C# 中不使用 linq 来执行此操作。如何在不使用 C# 中的 linq 的情况下使用特定 id 将数据表拆分为多个数据表?

foreach (DataRow row in dsBindSubCategory.Tables[0].Rows)
{
    DataRow newRow = newDt.NewRow();
    newRow.ItemArray = row.ItemArray;
    newDt.Rows.Add(newRow);
    i++;

    if (Convert.ToInt32(dsBindSubCategory.Tables[0].Rows[i]["ClientId"]) != Convert.ToInt32(dsBindSubCategory.Tables[0].Rows[i - 1]["ClientId"]))
    {
        newDs.Tables.Add(newDt);
        j++;
        newDt = dsBindSubCategory.Tables[0].Clone();
        newDt.TableName = "Table_" + j;
        newDt.Clear();
        i = 0;
    }
}

return newDs;

【问题讨论】:

  • 出于好奇,为什么要避免使用 linq?
  • 通过使用这个,List subTables = (dsBindSubCategory.Tables[0]).AsEnumerable().GroupBy(row => row.Field("ClientId")).Select (g => g.CopyToDataTable()).ToList();我达到了我的结果。我无法将这些结果绑定到 gridview 中。
  • 等一下,你想将多个DataTables 绑定到一个GridView
  • 这实际上开始听起来像XY Problem。你的最终游戏是什么?你可能走错了方向。
  • 将数据集拆分为多个数据表并创建动态gridview并绑定它们

标签: c# asp.net datatable dataset datarow


【解决方案1】:

您可以创建 DataViews 并将它们复制到 dataTables:

DataView DataView1 = new DataView(table,"ClientId=1","ClientId ASC", DataViewRowState.CurrentRows);
DataTable dt1 = DataView1.Table.Clone(); 

【讨论】:

  • 主数据表clientId是动态的,可以是2个以上。基于那些我想创建多个数据表。那么我该怎么做。
  • 您可以动态更改过滤器表达式,如 WHERE SQL 子句,即:new DataView (table,Expression, ..., ...)
【解决方案2】:

以下代码将从一个数据表创建两个数据表。请注意,我也在代码中创建原始表,因为我无权访问您的数据源。

    DataTable table = new DataTable();

    DataColumn col1 = new DataColumn("clientid");
    DataColumn col2 = new DataColumn("clientname");

    col1.DataType = System.Type.GetType("System.Int32");
    col2.DataType = System.Type.GetType("System.String");

    table.Columns.Add(col1);
    table.Columns.Add(col2);

    DataRow r = table.NewRow();
    r[col1] = 1;
    r[col2] = "client 1";
    table.Rows.Add(r);
    r = table.NewRow();
    r[col1] = 1;
    r[col2] = "client 2";
    table.Rows.Add(r);
    r = table.NewRow();
    r[col1] = 2;
    r[col2] = "client 3";
    table.Rows.Add(r);

    // Create two new data tables
    DataTable dt1 = new DataTable("t1");
    DataTable dt2 = new DataTable("t2");
    // Make the columns of the new tables match the existing table columns
    foreach(DataColumn dc in table.Columns)
    {
        dt1.Columns.Add(new DataColumn(dc.ColumnName, dc.DataType));
        dt2.Columns.Add(new DataColumn(dc.ColumnName, dc.DataType));
    }

    foreach (DataRow row in table.Rows)
    {
        int id = Convert.ToInt32(row["clientid"]);
        if (id == 1)
        {
            DataRow dr = dt1.NewRow();
            dr.SetField("clientid", row["clientid"]);
            dr.SetField("clientname", row["clientname"]);
            dt1.Rows.Add(dr);
        }
        else
        {
            DataRow dr = dt2.NewRow();
            dr.SetField("clientid", row["clientid"]);
            dr.SetField("clientname", row["clientname"]);
            dt2.Rows.Add(dr);
        }
    }
}

所以,显然这只是创建了两个表。由于您的评论说您需要为每个唯一 ID 提供多个新数据表,因此您需要使用以下代码:

    DataTable newTable= new DataTable();
    // Make the columns of the new tables match the existing table columns
    foreach(DataColumn dc in table.Columns)
    {
        newTable.Columns.Add(new DataColumn(dc.ColumnName, dc.DataType));
        newTable.Columns.Add(new DataColumn(dc.ColumnName, dc.DataType));
    }

每次您遍历源表中的行并找到一个新的客户端 ID。您可能需要某种 id 字典和新的结果表来跟踪事物。如果您需要更多详细信息,我可以尝试编写一个更完整的示例,但概念是相同的。

【讨论】:

  • 主数据表clientId是动态的,可以是2个以上。基于那些我想创建多个数据表。那么我该怎么做。
  • 但是我的主表中有 17 列。每次创建拆分表时,我都想提及“newTable.Columns.Add(new DataColumn(dc.ColumnName, dc.DataType)) ;" .有没有其他解决方案。现在我正在尝试比较当前行和数据行中的下一行,如果下一行中的 clientid 不同,然后拆分为单独的数据表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-21
  • 1970-01-01
相关资源
最近更新 更多