Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用c.net将数据表行与6列分组_C#_Datatable - Fatal编程技术网

C# 如何使用c.net将数据表行与6列分组

C# 如何使用c.net将数据表行与6列分组,c#,datatable,C#,Datatable,我需要帮助。我在一个数据表中有6列。我已经将其转换为一个dataview,并按所有六个对其进行排序,然后相应地更新了datatable。当最后4列中的值相同时,我需要对行进行分组,并将它们放在它们自己的、唯一的表中,以便以后使用,从而将它们从原始表中删除 我的列是:CurveNumber、ObjectId、Length、Radius、Delta和Tangent 感谢您提供的帮助。方法-从DataView开始,使用其.ToTable方法首先获取最后四列的唯一值集合。然后在原始表中循环查找匹配项源代

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

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


感谢您提供的帮助。

方法-从DataView开始,使用其.ToTable方法首先获取最后四列的唯一值集合。然后在原始表中循环查找匹配项源代码::

我想指出,使用LINQ很可能有一个更优雅的解决方案。不过,这会让您达到您需要的位置。

这里有另一个解决方案


你的要求不清楚我可以添加什么来帮助你澄清?提供示例输入和输出会很有帮助。kk会看看我能做什么…会有几百行谢谢你。工作得很好。
    // 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);
    }
DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn("CurveNumber"), new DataColumn("ObjectId"), new DataColumn("Length"), new DataColumn("Radius"), new DataColumn("Delta"), new DataColumn("Tangent") }); 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>