Linq for joining 2 columns

Hi Team,

I am using 1 query that groups and joins column 13 at the same condition and in the same query i want column 12 joined as well.

(From d In InputFile.AsEnumerable() Group d By k1 = d(6).ToString().Trim(), k2 = d(8).ToString().Trim() Into grp = Group Let s = String.Join(“,”, grp.Select(Function(x) x(13).ToString())) Let ra = New Object() {k1, k2, s} Select joiningDT.Rows.Add(ra)).CopyToDataTable()
Please help team.

Hi @yashchoursia25

(From d In InputFile.AsEnumerable()
Group d By k1 = d(6).ToString().Trim(), k2 = d(8).ToString().Trim(), k3 = d(12).ToString().Trim() Into grp = Group
Let s = String.Join(“,”, grp.Select(Function(x) x(13).ToString()))
Let ra = New Object() {k1, k2, k3, s}
Select joiningDT.Rows.Add(ra)).CopyToDataTable()

or

(From d In InputFile.AsEnumerable()
Group d By k1 = d(6).ToString().Trim(), k2 = d(8).ToString().Trim(), k3 = d(11).ToString().Trim() Into grp = Group
Let s12 = String.Join(“,”, grp.Select(Function(x) x(12).ToString()))
Let s13 = String.Join(“,”, grp.Select(Function(x) x(13).ToString()))
Let ra = New Object() {k1, k2, k3, s12, s13}
Select joiningDT.Rows.Add(ra)).CopyToDataTable()

Hope it helps!!

Hi @yashchoursia25

Please try this

(From d In InputFile.AsEnumerable()
 Group d By k1 = d(6).ToString().Trim(), k2 = d(8).ToString().Trim()
 Into grp = Group
 Let s13 = String.Join(",", grp.Select(Function(x) x(13).ToString()))
 Let s12 = String.Join(",", grp.Select(Function(x) x(12).ToString()))
 Let ra = New Object() {k1, k2, s12, s13}
 Select joiningDT.Rows.Add(ra)).CopyToDataTable()

Regards,

1 Like
(From d In InputFile.AsEnumerable() 
Group d By k1 = d(6).ToString().Trim(), k2 = d(8).ToString().Trim() Into grp = Group 
Let s = String.Join(“,”, grp.Select(Function(x) x(13).ToString())) 
Let r = String.Join(“,”, grp.Select(Function(x) x(12).ToString())) 
Let ra = New Object() {k1, k2, s,r} 
Select joiningDT.Rows.Add(ra)).CopyToDataTable()

Or

(From d In InputFile.AsEnumerable() 
Group d By k1 = d(6).ToString().Trim(), k2 = d(8).ToString().Trim() Into grp = Group 
Let s12 = String.Join(“,”, grp.Select(Function(x) x(12).ToString())) 
Let s13 = String.Join(“,”, grp.Select(Function(x) x(13).ToString())) 
Let s = s12 + "," + s13
Let ra = New Object() {k1, k2, s} 
Select joiningDT.Rows.Add(ra)).CopyToDataTable()

@lrtetala how can i join only non null values here. Please help.

(From d In InputFile.AsEnumerable()
 Where d(12).ToString isNot Nothing and d(13).ToString isNot Nothing 
 Group d By k1 = d(6).ToString().Trim(), k2 = d(8).ToString().Trim()
 Into grp = Group
 Let s13 = String.Join(",", grp.Select(Function(x) x(13).ToString()))
 Let s12 = String.Join(",", grp.Select(Function(x) x(12).ToString()))
 Let ra = New Object() {k1, k2, s12, s13}
 Select joiningDT.Rows.Add(ra)).CopyToDataTable()

not working @Quenton_Wayne_Rebello
Output:

See it is joining the blank ones as well

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