Maximum value of a datatable row

is there any way to find the maximum value of a given datatable row? I don’t want to find the maximum value of a column, but of a row. Thanks

Here an example: I sould extract the value “55,80”. I thought about a switch case cycle combined with sget value() but i would like to know whether there is a more elegant solution

Hi @massimilianolorenzin

The maximum number of rows in datatable is 16,777,216

For counting the row in datatable,

dtTable.rows.count

Thanks

Hi,

Can you try the following expression?

dt.Columns.Cast(Of DataColumn).Max(Function(c) Int32.Parse(dt.Rows(0)(c).ToString))

This returns maximum value in the row #0 as int32.

Regards,

Hi, thanks for answering. I’m not talking about the number of rows, but the value of a given row (which I have already extracted correctly)

@massimilianolorenzin are you referring to a cell in a datatable?

If yes then row(0).tostring.length will give the length of the string for the first column of the row.

If no can you please give some examples…

Thanks

I need to select the maximum value of this row. I thought about a switch case combined with get value() but i would like to know whether there is a more elegant solution

Hi,

Can you try the following steps?

First, read the table to DataTable using ReadRange activity. (Let’s say dt)
Then, the following expression will return max value of the row #0. Please modify to row number which you want to calculate

dt.Columns.Cast(Of DataColumn).Max(Function(c) if(Double.TryParse(dt.Rows(0)(c).ToString,New Double),Double.Parse(dt.Rows(0)(c).ToString),Double.MinValue))

Regards,

2 Likes

Hi

Let me try to keep it very simple

Say for example you have read the data with read range activity and saved it with variable named dt

Now use a assign activity like this to get the max value of a specific row

str_max = dt.Rows(0).ItemArray.Max.ToString

Where 0 is the row index of first row in a DATATABLE (header is not considered as first row)

Or

If you are in a for each row loop then mention like this inside that loop

str_max = dt.Rows(dt.Rows.IndexOf(row)).ItemArray.Max.ToString

This will automatically get the rowindex and give you the max value Alone

If you want minimum value then mention as Min

Cheers @massimilianolorenzin

2 Likes

Yes, as the following using Assign activity.

doubleVar = dt.Columns.Cast(Of DataColumn).Max(Function(c) if(Double.TryParse(dt.Rows(0)(c).ToString,New Double),Double.Parse(dt.Rows(0)(c).ToString),Double.MinValue))
1 Like

Yeah we can assign it to a string variable or any type you would like to

For now it’s assigned to string variable

Cheers @massimilianolorenzin

2 Likes

Thank you. I was looking for something like that. Uipath throws an error " the object must be a double". I tried to convert dt.Rows(0).ItemArray.Max to a double with convert.toDouble but it doesn’t work

Hi @massimilianolorenzin,

Try using DoubleVar = CDbl(YourVar) to convert to Double

Will this work in case above where the first column is not number but alphanumeric value?

Cheers

I tried
cdbl(dt_listinoBandeCompleto.Rows(RowIndex).ItemArray.Max)
but i Get the same error

Hi. it throws this error “Assign: Input string was not in a correct format.”

Hi. It Works. Thank you very much. Could you explain me the formula? you first convert the columns in DataColumn, the you apply the max function. In the max function why do you write “Function(c)” ? and then you try to convert the max value into a double value, if true you convert it into a double value, otherwise you convert it into the minimum value of double? why do you do so?

Try the following modified solution of @Palaniyappan

str_max = dt.Rows(0).ItemArray.Where(function(x) double.TryParse(x.ToString, new Double)).Max.ToString

the “Where” clause will filter out anything that is not double and therefore eliminate the error

Cheers

1 Like

Hi,

In the max function why do you write “Function(c)”

It’s lambda expression of LINQ. We need to think it as one of syntax of VB.net.

and then you try to convert the max value into a double value, if true you convert it into a double value, otherwise you convert it into the minimum value of double?

In this case, we need to get max value. So, if the invalid data is converted to Double minimum value, it won’t be selected as max value.
And, it’s also good way to use Where method to achieve it, as @J0ska mentioned.

Regards,

2 Likes

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