Consolidate Rows (Type String) in Data Table

Hi

The below example is a datatable. The datatable contains 5 records showing Lived cities and countries by John, Sam and Rohit.

Name, Lived Cities, Lived Countries
John, Kiev, Ukraine
John, KL, Malaysia
Sam, NY, USA
Rohit, Mumbai, India
Rohit, Bali, Indonesia

I want to consolidate the rows in the datatable as below. That means

Name, Lived Cities, Lived Countries
John, Kiev, KL Ukraine, Malaysia
Sam, NY, USA
Rohit, Mumbai, Bali India, Indonesia

Is there a solution using dt.rows.add?

Hi,

Hope the following sample helps you.

dt = dt.AsEnumerable.GroupBy(Function(r) r("Name").ToString).Select(Function(g) dt.Clone.LoadDataRow({g.Key,String.Join(",",g.Select(Function(r) r("Lived Cities").ToString)),String.Join(",",g.Select(Function(r) r("Lived Countries").ToString))},False)).CopyToDataTable()

Sample20220226-1.zip (2.5 KB)

Regards,

2 Likes

Works like a boss, thank you so much.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.