Trim spaces while comparing datatable columns

Hi,
The following code is working except that it is not trimming values and takes as a mismatch. How to fix it to trim column values to compare? It is finding rows in dt1 and dt2 which differ in number of spaces. Thank you so much!

dtResult=dt1.AsEnumerable().Except(dt2.AsEnumerable(),System.Data.DataRowComparer.Default).CopyToDataTable

Hi @A_Learner ,

An Update maybe required on the Datatables and then we need to perform the comparison operation. One of the ways to perform the Trim operation update is mentioned below :

Somehow these two solutions provided above did not work for me. It still lists columns with spaces as differences. Thank you,

@A_Learner ,

We can now ask more details on what/How the implementation was done, so we can find out on where it is failing.

dt3Corrected = (From r In dt3.AsEnumerable
Select ia = r.ItemArray.toList
Select ic = ia.ConvertAll(Function (e) e.ToString.Trim()).toArray()
Select dt3Corrected.Rows.Add(ic)).CopyToDataTable()

dt4Corrected = (From r In dt4.AsEnumerable
Select ia = r.ItemArray.toList
Select ic = ia.ConvertAll(Function (e) e.ToString.Trim()).toArray()
Select dt4Corrected.Rows.Add(ic)).CopyToDataTable()

dtRow = dt3Corrected.AsEnumerable().Except(dt4Corrected.AsEnumerable(),System.Data.DataRowComparer.Default)

if dtRow.ToList.Count > 0
dtResult = dt3.AsEnumerable().Except(dt4.AsEnumerable(),System.Data.DataRowComparer.Default).CopyToDataTable

@A_Learner ,

Is dtResult assigned the below when the count > 0:

dtResult = dtRow.CopyToDatatable

Yes, @supermanPunch that code line is in place. It is not working as mentioned.
I am getting false alarm.

Thank you,

@A_Learner ,

We don’t need this to be done because we are already using dt3Corrected and dt4Corrected the corrected datatables.

So, we just need to use dtRow and convert it to Datatable when the count > 0 :

Thank you, updated my code. Result did not improve.

@A_Learner ,

Now we can ask for the Data used if possible to provide. So we can check the implementation for the same set of data from our end.

Thank you, @supermanPunch
I find it to be working after a closer look at data.
Marked the above as solution. Appreciate it.

1 Like

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