Linq to .trim all values in datatable’s all rows & all columns?

Linq to .trim all values in datatable’s all rows & all columns?

With easy to understand variable name so i can understand.

2 Likes

Hi @wija

Use the below Invoke Code,

newDt = New DataTable()

For Each col As DataColumn In dt.Columns
    newDt.Columns.Add(col.ColumnName, col.DataType)
Next

' Populate the new DataTable with trimmed values
For Each row In dt.AsEnumerable()
    Dim newRow As DataRow = newDt.Rows.Add()
    For Each col As DataColumn In dt.Columns
        Dim value = row(col)
        If TypeOf value Is String Then
            newRow(col.ColumnName) = DirectCast(value, String).Trim()
        Else
            newRow(col.ColumnName) = value
        End If
    Next
Next

Invoked Arguments:

Hope it helps!!

1 Like

Hi,

Try this:-
In invoke code activity.

dataTable.AsEnumerable().ToList().ForEach(Sub(row) row.ItemArray = row.ItemArray.Select(Function(item) If(item IsNot DBNull.Value, item.ToString().Trim(), item)).ToArray())

Thanks

1 Like

With two Assign statements:

trimmedDataTable = originalDataTable.Clone
trimmedDataTable = originalDataTable.AsEnumerable.Select(Function(row) row.ItemArray.Select(Function(cell) cell.ToString().Trim()).ToArray()).Select(Function(cells) trimmedDataTable.Rows.Add(cells)).CopyToDataTable
2 Likes

This one seems easy to understand & make sense.
Will try this tomorrow, sleeping now :laughing:

1 Like

Variation - Handles only String Datatype Columns and keeps other DataTypes

(From d In dtData.AsEnumerable()
Let ra =d.ItemArray.Select(Function (x, i) If(d.Table.Columns(i).DataType.Equals( GetType( String )), x.ToString().Trim() , x)).ToArray()
Select r = dtCorrected.Rows.Add(ra)).CopyToDataTable()

taken from:

1 Like

I don’t understand the ra ia i etc etc x.x

1 Like

all ra, ia …are used like local variable names

have a look at the different LINQ Starter helps, as we recommend to have certain LINQ knowledge when using LINQ within implementations
[HowTo] LINQ (VB.Net) Learning Catalogue - Help / Something Else - UiPath Community Forum

1 Like

ok this one works with easy to understand variable name.

1 Like

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