Reputation: 13
Using VB, I've used the following line to successfully find the minimum value in a specific column (say column 5, where the values are all of double) in a datatable:
Dim test as double
test = datatable.Compute("min(sourcecolumn)", "")
I would now like to refer to the values in other columns (let's say column 2) along the row containing that minimum column value.
Any help would be much appreciated as I can't get my head round it!
Thanks
Upvotes: 0
Views: 172
Reputation: 3007
You can use the DataTable.Select()
method to get the row(s) that contain the minimum value. DataTable.Select()
returns a DataRow()
. In the code below, I assumed only one column contains the minimum value hence Data(0)
.
Dim test as double
test = datatable.Compute("min(sourcecolumn)", "")
Dim Data() As DataRow = datatable.Select("sourcecolumn = " & test.ToString())
Dim column2 = Data(0)(1)
Upvotes: 1
Reputation: 54427
All you have is a value but you currently have no idea what row(s) contain that value. You can use the table's Select
method to get the row(s) that contain that value in that column. Once you have the row(s), you can do whatever you want with it/them:
Dim minValue = CDbl(myDataTable.Compute("MIN(MyColumn)", Nothing))
Dim rows = myDataTable.Select($"MyColumn = {minValue}")
For Each row In rows
'Use row here.
Next
Select
always returns an array, even if there is only one row, so the loop will always work. If you know that there will never be more than one match, you can just get the first element directly.
Upvotes: 0