Search Maximum Value in Column2 of DataTable and and Retrieve Column1 Data

Hi Everyone

I have a Data table consisting of 2 columns and 5000+ rows.

I like to avoid using a for each loop as it will be time consuming so is there an efficient way of searching the WHOLE data table to see which row has the hightest int32 value and get the value from corresponding Column1

Thanks

@CGhoST,

You can get the desired value like this.

Get Maximum value:

intMaxValue = dtInput.AsEnumerable().Max(Function(r) CInt(r.Field(Of Object)("Col2").ToString()))

Get Column 1 of the maximum value

strOutput = dtInput.AsEnumerable().Where(Function(r) r.Field(Of Integer)("Col2") = intMaxValue).Select(Function(r) r.Field(Of String)("Col1")).FirstOrDefault()

Output:

Sample Code:
MaxCountDemo.zip (33.9 KB)

Is it possible to do in just one assign activity?

@CGhoST,

Yes, use this LINQ

dtInput.AsEnumerable().OrderByDescending(Function(r) r.Field(Of Object)("Col2").ToString()).Select(Function(r) r.Field(Of String)("Col1")).FirstOrDefault()

This is perfect.

Thank you

1 Like

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