Ugh, be prepared for an accident that makes a mess of your data with this “associated by position” idea
I wouldn’t use LINQ for this either
var l = new List<string>();
string prev = null;
foreach(DataRow r in dt.Rows){
var s = (string)r["Column1"];
var t = (string)r["Column2"];
if(s != prev){
l.Add(s);
prev = s;
}
l.Add(t);
}
Desperate for something that uses LINQ?
var l = new List<string>();
var gg = dt.Rows.Cast<DataRow>().GroupBy(r => (string)r["Column1"]);
foreach(var g in gg){
l.Add(g.Key);
foreach(var r in g)
l.Add((string)r["Column2"]);
}
So you want the output as a datatable – add to a datatable instead of a list:
var l = new DataTable();
l.Columns.Add("X");
string prev = null;
foreach(DataRow r in dt.Rows){
var s = (string)r["Column1"];
var t = (string)r["Column2"];
if(s != prev){
l.Rows.Add(s);
prev = s;
}
l.Rows.Add(t);
}
8
solved how to download gridview in excel in ASP.NET C #, ie How to use group by in datatable