Hi RitwikSaha,
Refer below code.
C#
DataTable dtOriginal = new DataTable();
dtOriginal.Columns.AddRange(new DataColumn[]
{
new DataColumn("Id"),
new DataColumn("Amount 1"),
new DataColumn("Amount 2"),
new DataColumn("Amount 3")
});
dtOriginal.Rows.Add(1, 2, 2, 2);
dtOriginal.Rows.Add(12, 4, 6, 4);
dtOriginal.Rows.Add(12, 6, 6, 5);
dtOriginal.Rows.Add(22, 7, 2, 1);
dtOriginal.Rows.Add(22, 7, 2, 2);
DataTable dtFinal = dtOriginal.AsEnumerable()
.GroupBy(r => r["Id"])
.Select(x =>
{
var row = dtOriginal.NewRow();
row["Id"] = x.Key;
row["Amount 1"] = x.Sum(r => Convert.ToInt32(r["Amount 1"]));
row["Amount 2"] = x.Sum(r => Convert.ToInt32(r["Amount 2"]));
row["Amount 3"] = x.Sum(r => Convert.ToInt32(r["Amount 3"]));
return row;
}).CopyToDataTable();
VB.Net
Dim dtOriginal As DataTable = New DataTable()
dtOriginal.Columns.AddRange(New DataColumn() {
New DataColumn("Id"),
New DataColumn("Amount 1"),
New DataColumn("Amount 2"),
New DataColumn("Amount 3")})
dtOriginal.Rows.Add(1, 2, 2, 2)
dtOriginal.Rows.Add(12, 4, 6, 4)
dtOriginal.Rows.Add(12, 6, 6, 5)
dtOriginal.Rows.Add(22, 7, 2, 1)
dtOriginal.Rows.Add(22, 7, 2, 2)
Dim dtFinal As DataTable = dtOriginal.AsEnumerable() _
.GroupBy(Function(r) r("Id")) _
.Select(Function(x)
Dim row = dtOriginal.NewRow()
row("Id") = x.Key
row("Amount 1") = x.Sum(Function(r) Convert.ToInt32(r("Amount 1")))
row("Amount 2") = x.Sum(Function(r) Convert.ToInt32(r("Amount 2")))
row("Amount 3") = x.Sum(Function(r) Convert.ToInt32(r("Amount 3")))
Return row
End Function).CopyToDataTable()