How to get maximum length of a string

How to get maximum length of a string for a particular column in a datatable

Hi @Prem_Chand1 ,


Using this you can get it

or if you need maxlength of your column:


Hi @Prem_Chand1

You can use the following query in the Assign activity to get the desired result:

maxLength = dataTable.AsEnumerable().Max(Function(row) row.Field(Of String)("ColumnName").Length)

Here, maxLenght is a variable of type Int32.

Hope this helps,
Best Regards.

When: the longest string in the column
Then: as mentioned above with the usage of LINQ Max Operator

When: the data column config setting is meant
Then: YourDataTableVar.Columns(ColNameOrIndex).MaxLength

Thanks so much I tried many ways but it could saved my day :grinning::grinning::grinning:

1 Like

Can you suggest best way to learn linq @arjunshenoy

I tried this way it will give -1

It might be the case that the maxlength is not set for that column, that means it could take a string of any length.

You would have read the DataTable from some source, hence this -1 is coming.


have a look here


The following resources might help you with the learning:

Best Regards.

Hi @Prem_Chand1

Use the Len function in an If condition to compare the length of the current value with the maxLength variable. If the length is greater, update maxLength with the new value.

Assign activity: maxLength = 0

For Each Row activity (DataTable = yourDataTable)
Assign activity: currentValue = row(“ColumnName”).ToString()
Assign activity: currentLength = Len(currentValue)
If activity: currentLength > maxLength
Assign activity: maxLength = currentLength