【发布时间】:2011-08-01 13:18:21
【问题描述】:
我有一个来自 db 的 DataTable[有 5 列],其中包含组合数据。 我需要在这个组合表上调用 group by 并创建 2 个表...一个具有 groupedBy 行和其他具有项目。 在 C# 代码中执行此操作的最快方法是什么? 另外,我在下面编写了为这两个表添加列的代码。正确吗?
这是我的代码:
string colName = "ACCOUNT_ID";
var allRows = combinedTable.AsEnumerable();
var accountRowGroups = allRows.GroupBy(row => row[colName]);
DataTable masterDataTable = new DataTable();
DataTable childPricesDataTable = new DataTable();
// Create the columns
DataColumnCollection pdCols = combinedTable.Columns;
for (int ndx = 0; ndx < pdCols.Count; ndx++)
{
string columnName = pdCols[ndx].ColumnName;
Type type = Type.GetType(pdCols[ndx].DataType.ToString());
masterDataTable.Columns.Add(columnName, type);
childPricesDataTable.Columns.Add(columnName, type);
}
【问题讨论】: