Need Linq for Find duplicate data and merge the corresponding data of other cell

Hello , Here I mentioned the sample input of my reqirement, where I need to find duplicate in “RollNo” and get thier corresponding values in Column “Name” and Add the string into single cell of Column “Name”
Input
image

Output (required)
Name,RollNo
Ajay,1
Dinesh Siva,5
Siva,2
Pradeep,4

Linq I used
(From G In BuildDT.AsEnumerable
Group By O = New With {Key.K = G.Item(“RollNo”), Key.U = G.Item(“RollNo”)}
Into Group Select Group(0)).ToArray().copytodatatable()

Kindly Provide me a solution

Thanks in advance

Regards,
Gokul Jai

Hey @Gokul_Jayakumar,

You can use the below code,

(From row In dt1.AsEnumerable
	Group row By key = New With { 
	Key.name = row.Item("Name")} Into grp = Group
	Select dt1Clone.LoadDataRow(New Object() {
		key.name,
		String.Join(" ",grp.Select(Function(x)x("Address").ToString).ToArray)
	}, True)).CopyToDataTable

Thanks,
Sanjit

1 Like

Assumption - dtResult = BuildDT.Clone or prepared datatable with e.g. build datatable

we can simplify to


(From d In BuildDT.AsEnumerable()
Group d By k=d("RollNo").toString().Trim() Into grp = Group
Let nj = String.Join(",", grp.Select(Function (n) n("Name").toString.Trim))
Let ra = new Object(){nj,k}
Select r = dtResult.Rows.Add(ra)).CopyToDataTable
1 Like

@ppr @Sanjit_Pal , Thanks for your valuable inputs.

@ppr , Thanks for your instatnt response, from your expression I can get what i expected, but only 2 columns only I’m able to get data.
In case of multiple columns having same row data values except one column(which has different data) how can use your Expression and add the different data string. Same scenerio I already mentioned above, That is 2 column, I need it for 7 columns, where 6 columns are unique and 7 th column is Different data , need to add all string in single cell and remove other

I attached my sample Excel sheets here , it contains 2 sheets of input and output sheets
Sample (4).xlsx (9.1 KB)

your excel is different to the provided sample from above, therefore the grouping goes different

what are all the columns you want to group by the data?

Also have a look here:

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