Hello everyone.
I got an excel like this
Name Col1 Col2
Apple 10 15
Orange 15 20
Apple1 2 3
Orange1 8 10
And the output I want is
Name Col1 Col2
Apple 8 12
Orange 7 10
Apple1 2 3
Orange1 8 10
like Apple = Apple-Apple1 , Orange=Orange-Orange1
Is it possible with Linq or smth?
dt = (From row In dt.AsEnumerable()
Let commonPart = System.Text.RegularExpressions.Regex.Match(row(“Name”).ToString(), " [1]+ ").Value
Group row By commonPart Into Group
Let totalCol1 = Group.Sum(Function(r) Convert.ToInt32(r(“Col1”)))
Let totalCol2 = Group.Sum(Function(r) Convert.ToInt32(r(“Col2”)))
Select dt.Rows.Add(commonPart, totalCol1, totalCol2)).CopyToDataTable()
This LINQ statement groups the rows by the common part of the “Name” column, calculates the sum of “Col1” and “Col2” for each group, and then creates a new DataTable with the updated values.
How about its for every column instead of only Col1,Col2?
The following part is for Col2. So if you need to add other column, please replace Col2 with the column name then add it after the col2 part with comma.