如何使用 c#.net 对具有 6 列的数据表行进行分组
How to Group Datatable rows with 6 columns with c#.net
需要帮助。我在数据 table 中有 6 列。我已将其转换为数据视图,并按所有六个对其进行排序,然后相应地更新数据table。当最后 4 列中的值相同时,我需要对行进行分组,并将它们放在它们自己的、唯一的 table 中,我以后可以使用它们,将它们从原始 table.[=10= 中删除]
我的列是:CurveNumber、ObjectId、Length、Radius、Delta 和 Tangent。
感谢您提供的任何帮助。
一种方法 - 从 DataView 开始并使用其 .ToTable() 方法首先获取最后四列的唯一值集合。然后循环查找原始 table 中的匹配项(来源: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 的更优雅的解决方案。但是,这应该可以让您到达需要的位置。
这里有另一个解决方案
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>
需要帮助。我在数据 table 中有 6 列。我已将其转换为数据视图,并按所有六个对其进行排序,然后相应地更新数据table。当最后 4 列中的值相同时,我需要对行进行分组,并将它们放在它们自己的、唯一的 table 中,我以后可以使用它们,将它们从原始 table.[=10= 中删除]
我的列是:CurveNumber、ObjectId、Length、Radius、Delta 和 Tangent。
感谢您提供的任何帮助。
一种方法 - 从 DataView 开始并使用其 .ToTable() 方法首先获取最后四列的唯一值集合。然后循环查找原始 table 中的匹配项(来源: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 的更优雅的解决方案。但是,这应该可以让您到达需要的位置。
这里有另一个解决方案
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>