Etienne - 1 year ago 77
Vb.net Question

# Min and max from list collection

How can I get the min value ad max value from this example

``````Public Class RowsFound
Property RowIndex As integer
Property Qty As Integer
Property LineValue As Double
End Class
Dim Test as new List(Of RowsFound)
``````

above you can see the structure to my list. This is what the data would look like.

What would be the best way to get the RowIndex based on max LineValue and RowIndex of Min LineValue

I have done this as a test but would like to see if there a better way of doing this.

``````                        Dim MaxRow As Integer = 0
Dim MaxRowValue As Integer = 0
Dim MinRow As Integer = 999999
Dim MinRowValue As Integer = 999999
For Each MinMaxitem As RowsFound In ListOfRows
If MinMaxitem.LineValue < MinRowValue Then
MinRow = MinMaxitem.RowIndex
MinRowValue = MinMaxitem.LineValue
End If
If MinMaxitem.LineValue > MaxRowValue Then
MaxRow = MinMaxitem.RowIndex
MaxRowValue = MinMaxitem.LineValue
End If
Next
``````

Thank you :)

An easy way to do this would be to use LINQ:

``````Public Class RowsFound
Property RowIndex As Integer
Property Qty As Integer
Property LineValue As Double
Public Sub New(i As Integer, q As Integer, v As Double)
Me.RowIndex = i
Me.Qty = q
Me.LineValue = v
End Sub
End Class
Dim Test As New List(Of RowsFound) From {New RowsFound(0, 1, 105.25), New RowsFound(1, 2, 100), New RowsFound(2, 1, 110), New RowsFound(3, 2, 60.25)}

Dim RowIndexMax As Integer = (From row As RowsFound In Test.OrderByDescending(Function(x) x.LineValue) Select row.RowIndex).First
Dim RowindexMin As Integer = (From row As RowsFound In Test.OrderBy(Function(x) x.LineValue) Select row.RowIndex).First
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download