我有以下格式的数据表
Country Currency Category Value (Delivered)
AFRICA USD CONTAINER 100
AFRICA USD CONTAINER 100
AFRICA USD PLASTIC 100
AFRICA USD PLASTIC 100
AFRICA USD PLASTIC 100
AFRICA USD PLASTIC 100
AFRICA USD PLASTIC 100
AFRICA USD PLASTIC 100
我需要获得如下的值,
COUNTRY Currency CONTAINER PLASTIC OTHERS
Africa USD 200 600 0
意味着,塑料或容器以外的任何东西都需要添加到其他栏目中。
答案 0 :(得分:0)
您可以为此目的使用NReco.PivotData nuget软件包:
DataTable tbl; // lets assume this is your datatable
var pvtData = new PivotData(new[] {"Country", "Currency", "Category"}, new SumAggregatorFactory("Value (Delivered)") );
pvtData.ProcessData(new DataTableReader(tbl));
var pvtTbl = new PivotTable(
new [] {"Country", "Currency"}, //rows
new [] {"Category"}, //columns
pvtData);
// use PivotTable class API to iterate through rows/columns and get the values
for (var r=0; r<pvtTbl.RowKeys.Length; r++) {
var rowKey = pvtTbl.RowKeys[r];
for (var c=0; c<pvtTbl.ColumnKeys.Length; c++) {
var cellValue = pvtTbl[r,c].Value;
// do what you need: render HTML table, or export to CSV, or populate pivoted DataTable
}
}
对于非SaaS一次性部署项目,图书馆是免费的(我是该库的作者)。