我有一个数据表,并将该数据表转换为另一个具有行名称为列名的数据透视表。我在数据中缺少一些行名,因此也需要在列名中包括这些行名。
我成功地将行动态转换为数据表中存在的列。我的数据中缺少提取类型“最早” 示例:
ID PickupType Pickupdate
1 Actual 12/10/2018
1 Planned 12/13/2018
1 Latest 12/13/2018
2 Actual 12/14/2018
2 Planned 12/15/2018
代码:
string[] uniquedropType = tblpickupdates.AsEnumerable().Select(x => x.Field<string>("PickupType")).Distinct().ToArray();
DataTable p_pickuptable = new DataTable();
p_pickuptable.Columns.Add("ID", typeof(string));
foreach (string colpickuptype in uniquedropType)
{
p_pickuptable.Columns.Add(colpickuptype, typeof(string));
}
var pickupgroups = tblpickupdates.AsEnumerable()
.GroupBy(x => new
{
ID = x.Field<string>("ID"),
}).ToList();
foreach (var pickupgroup in pickupgroups)
{
DataRow newRow = p_pickuptable.Rows.Add();
foreach (DataRow pickuptype in pickupgroup)
{
newRow[pickuptype.Field<string>("PickupType")] = pickuptype.Field<string>("Pickupdate");
}
我期望结果如下
ID | Actual | Planned | Latest | Earliest
-- |----------- | -----------| ----------- | --------
1 | 12/10/2018| 12/13/2018 | 12/13/2018 | NULL
2 | 12/14/2018 | 12/15/2018 | NULL | NULL