源数据 如图所示
// <summary>
/// DataTable扩展 列转行方法(2*2矩阵转换)
/// </summary>
/// <param name="dtSource">数据源</param>
/// <param name="columnFilter">逗号分隔 如SDateTime,PM25,PM10,O3,NO2,SO2,CO 第一个参数为行,其余为要转换的列</param>
/// <returns></returns>
public static DataTable PivotDatatableColToRow(this DataTable dtSource, string columnFilter)
{
var columns = columnFilter.Split(',');
DataTable dtFilter = dtSource.DefaultView.ToTable(false, columns);
DataTable dtResult = new DataTable();
var rowCount = dtFilter.Rows.Count;
var columnCount = columns.Length;
// 源数组的行数比DataTable的行数+1,, 加一行表头
object[,] arrSource = new object[rowCount + 1, columnCount];
// 目标数组的行数等于选择的列数,列数等于 源数据的行数+1, 加一列 属性名
object[,] arrResult = new object[columnCount, rowCount + 1];
// 原数组第一行写表头
for (int i = 0; i < columnCount; i++)
{
arrSource[0, i] = dtFilter.Columns[i].ColumnName;
}
// 源数据 每一行写 数据
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{
arrSource[i + 1, j] = dtFilter.Rows[i][j];
}
}
// 原数 转置到 目标数组
for (int i = 0; i < rowCount + 1; i++)
{
for (int j = 0; j < columnCount; j++)
{
arrResult[j, i] = arrSource[i, j];
}
}
// 创建 Datatable 的结构
for (int i = 0; i < rowCount + 1; i++)
{
if (!dtResult.Columns.Contains(arrResult[0, i].ToString())) {
dtResult.Columns.Add(arrResult[0, i].ToString());
}
}
List<string> valueList = new List<string>();
for (int i = 1; i < columnCount; i++)
{
for (int j = 0; j < rowCount + 1; j++)
{
valueList.Add(arrResult[i, j].ToString());
}
dtResult.Rows.Add(valueList.ToArray());
valueList.Clear();
}
return dtResult;
}
转换后的数据
DataTable newdt = dt.AsEnumerable().Where(p => p.Field<string>("City") == "武汉").CopyToDataTable().PivotDatatableColToRow("SDateTime,PM25,PM10,O3,NO2,SO2,CO");