I have a datatable(Dynamically generated) in C#.net coding and that is developed by other developer, so I can't change how data is getting stored into that datatable. So my question is I want to groupby one of the column(I know the name of this column) and concatenate all other rows into one row and convert the final result into same datatable object or similar datatable object which i generated using datatable.clone
I was able to do all this with the piece of code below, by only if I know the column names and number of column, but my situation is I don't know the column names and number of columns gets generated by dynamically generated datatable by other developer
//dTable is a datatable which I got, and it is generated dynamically //rdTable is where I want the final result to go
rdTable = dTable.Clone();
var result = dTable.AsEnumerable()
.GroupBy(x => new { User = x.Field<string>("User"), })
.Select(x => new { Hobby1 = String.Join(",", x.Select(z => z.Field<string>("Hobby1"))),
User = x.Key.User,
Hobby2 = String.Join(",", x.Select(z => z.Field<string>("Hobby2"))),
Hobby3 = String.Join(",", x.Select(z => z.Field<string>("Hobby3"))), });
foreach (var row in result) {
DataRow nRow = rdTable.NewRow();
nRow["User"] = row.User;
nRow["Hobby1"] = row.Hobby1.ToString();
nRow["Hobby2"] = row.Hobby2.ToString();
nRow["Hobby3"] = row.Hobby3.ToString();
rdTable.Rows.Add(nRow); }
but my column names are dynamic they can change to anything and number of columns can increase
Any help will be highly appreciated, I am kind of struck at this point