如何使用c#.net对6列的数据表行进行分组
本文关键字:数据表 6列 何使用 net | 更新日期: 2023-09-27 18:09:46
需要帮助。我有6列在一个数据表。我已经将其转换为数据视图,并按所有六个对其进行排序,然后相应地更新数据表。当最后4列中的值相同时,我需要分组行,并将它们放在自己的唯一表中,我可以稍后使用,从原始表中删除它们。
我的专栏是:curvennumber, ObjectId, Length, Radius, Delta和Tangent。
感谢您提供的帮助
另一个解决方案
DataTable dt = new DataTable();dt.Columns。AddRange(新数据列[]{新数据列(" curvennumber "),新数据列("ObjectId"),新数据列("Length"),new dataccolumn ("Radius"), new dataccolumn ("Delta"), new dataccolumn("正切")});dt.Rows。Add(新对象[]{"1","0851"ax,"20"、"20"、"20"、"20"});dt.Rows。Add(新对象[]{"2"、"0852 ab"、"20"、"20"、"20"、"20"});dt.Rows。Add(新对象[]{"3","0853交流","25","32","12","10"});dt.Rows。Add(新对象[]{"4"、"公元0854年","12","31","15"," 20 "});dt.Rows。Add(新对象[]{"5"、"0855 ca"、"20"、"20"、"20"、"20"});dt.Rows。Add(新对象[]{"6"、"公元0856年"、"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>
方法—从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的更优雅的解决方案。然而,这应该能让你到达你需要的地方。