【问题标题】:How to Group Datatable rows with 6 columns with c#.net如何使用 c#.net 对具有 6 列的数据表行进行分组
【发布时间】:2015-08-26 04:53:28
【问题描述】:

需要帮助。我在数据表中有 6 列。我已将其转换为数据视图,并按全部六个对其进行排序,然后相应地更新数据表。当最后 4 列中的值相同时,我需要对行进行分组,并将它们放在自己的唯一表中,以便以后使用,将它们从原始表中删除。

我的列是:CurveNumber、ObjectId、Length、Radius、Delta 和 Tangent。

感谢您提供的任何帮助。

【问题讨论】:

  • 你的要求不明确
  • 我可以添加什么来帮助您澄清它吗?
  • 提供样本输入和输出会很有帮助。
  • kk 看看我能做什么

标签: c# datatable


【解决方案1】:

这里有另一个解决方案

 DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[] { new DataColumn("CurveNumber"), new DataColumn("ObjectId"), new DataColumn("Length"),
                                                   新数据列(“半径”),新数据列(“增量”),新数据列(“切线”)});
            dt.Rows.Add(new object[] { "1","0851ax","20","20","20","20" });
            dt.Rows.Add(new object[] { "2", "0852ab", "20", "20", "20", "20" });
            dt.Rows.Add(new object[] { "3", "0853ac", "25", "32", "12", "10" });
            dt.Rows.Add(new object[] { "4", "0854ad", "12", "31", "15", "20" });
            dt.Rows.Add(new object[] { "5", "0855ca", "20", "20", "20", "20" });
            dt.Rows.Add(new object[] { "6", "0856ad", "25", "32", "12", "10" });

        //Group by distinct 4 column
      var GroupBy4ColumnDistinct =  dt.Rows.Cast<DataRow>()
        .ToLookup(x => (Convert.ToString(x["Length"]) + Convert.ToString(x["Radius"]) + Convert.ToString(x["Delta"]) + Convert.ToString(x["Tangent"])).GetHashCode())
        .Select(x => new { key = x.Key, values = x.Select(y => Convert.ToString(y["CurveNumber"])).ToList() }).ToList();

        // add new table to dataset. dataset contain 3 table as shown in our sample output
      DataSet ds = new DataSet();
      foreach (var item in GroupBy4ColumnDistinct)
      {
          DataView dv = new DataView(dt);
          dv.RowFilter = " CurveNumber in ( " + string.Join(",", item.values) + " )";
          ds.Tables.Add(dv.ToTable());
      }</pre>

【讨论】:

  • 谢谢。效果很好。
【解决方案2】:

一种方法 - 从 DataView 开始并使用其 .ToTable() 方法首先获取最后四列的唯一值集合。然后在原始表中循环查找匹配项(来源:https://dotnetfiddle.net/PlAZSi):

    // Initial table set up and population
    DataTable originalTable = new DataTable("originaltable");
    originalTable.Columns.Add("CurveNumber", (123).GetType());
    originalTable.Columns.Add("ObjectID", ("String").GetType());
    originalTable.Columns.Add("Length", (123).GetType());
    originalTable.Columns.Add("Radius", (123).GetType());
    originalTable.Columns.Add("Delta", (123).GetType());
    originalTable.Columns.Add("Tangent", (123).GetType());

    originalTable.Rows.Add(new object[] { 1, "0851ax", 20, 20, 20, 20} );
    originalTable.Rows.Add(new object[] { 2, "0852ab", 20, 20, 20, 20} );
    originalTable.Rows.Add(new object[] { 3, "0853ac", 25, 32, 12, 10} );
    originalTable.Rows.Add(new object[] { 4, "0854ad", 12, 31, 15, 20} );
    originalTable.Rows.Add(new object[] { 5, "0855ca", 20, 20, 20, 20} );
    originalTable.Rows.Add(new object[] { 6, "0856ad", 25, 32, 12, 10} );

    // Create a new datatable containing the unique values
    // for the four columns in question
    DataTable uniqueValues = (new DataView(originalTable))
                                .ToTable(true, new string[] {"Length", 
                                                             "Radius", 
                                                             "Delta", 
                                                             "Tangent"});

    // Create a DataSet of DataTables each one containing the grouped
    // rows based on matches on the four columns in question.
    DataSet groupedRows = new DataSet("groupedRows");
    foreach (DataRow uniqueValue in uniqueValues.Rows) {

        // Create the individual table of grouped rows based on the
        // structure of the original table
        DataTable groupTable = originalTable.Clone();
        groupTable.TableName = String.Format("{0}-{1}-{2}-{3}", 
                                             uniqueValue["Length"], 
                                             uniqueValue["Radius"], 
                                             uniqueValue["Delta"], 
                                             uniqueValue["Tangent"]);

        // Fetch the rows from the original table based on matching to the
        // unique combination of four columns
        DataRow[] groupRows = originalTable.Select(String.Format(" Length = {0} AND Radius = {1} AND Delta = {2} AND Tangent = {3} ", 
                                                                 uniqueValue["Length"], 
                                                                 uniqueValue["Radius"], 
                                                                 uniqueValue["Delta"], 
                                                                 uniqueValue["Tangent"]));

        // Add each matched row into the individual grouped DataTable
        foreach (DataRow groupRow in groupRows) {
            groupTable.Rows.Add(groupRow.ItemArray);
        }

        // Finally, add the DataTable to the DataSet
        groupedRows.Tables.Add(groupTable);
    }

我想指出,很可能有一个使用 LINQ 的更优雅的解决方案。但是,这应该可以让您到达您需要的地方。

【讨论】:

    猜你喜欢
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 2015-04-07
    • 1970-01-01
    • 2012-10-07
    相关资源
    最近更新 更多