Take a look at below example (created in LinqPad):
void Main()
{
DataTable srcdt = GetInitialData();
List<KeyValuePair<string, string>> data = srcdt.AsEnumerable()
.Select(x=>new KeyValuePair<string, string>(x[0].ToString(), x[1].ToString()))
.ToList();
DataTable dstdt = new DataTable();
List<string> cols = data.Select(x=> x.Key).Distinct().ToList();
dstdt.Columns.AddRange(cols.Select(x=>new DataColumn(x, typeof(string))).ToArray());
if(cols.Count != srcdt.Rows.Count)
Console.WriteLine("The number of columns does not equal to no. of rows in source table!");
else
{
DataRow dr = dstdt.NewRow();
foreach(KeyValuePair<string, string> kvp in data)
{
dr[kvp.Key] = kvp.Value;
}
dstdt.Rows.Add(dr);
}
dstdt.Dump();
}
DataTable GetInitialData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("name", typeof(string)),
new DataColumn("value", typeof(string))
});
dt.Rows.Add(new object[]{"n1", "v1"});
dt.Rows.Add(new object[]{"n2", "v2"});
dt.Rows.Add(new object[]{"n3", "v3"});
return dt;
}
Note: If there would be non-unique data, you'll be in trouble ;)