Linq to .trim all values in datatable’s all rows & all columns?
With easy to understand variable name so i can understand.
Linq to .trim all values in datatable’s all rows & all columns?
With easy to understand variable name so i can understand.
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!!
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
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
This one seems easy to understand & make sense.
Will try this tomorrow, sleeping now ![]()
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:
I don’t understand the ra ia i etc etc x.x
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
ok this one works with easy to understand variable name.
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.