列表集合中的最小值和最大值
Min and max from list collection
如何从此示例中获取最小值和最大值
Public Class RowsFound
Property RowIndex As integer
Property Qty As Integer
Property LineValue As Double
End Class
Dim Test as new List(Of RowsFound)
上面你可以看到我列表的结构。这就是数据的样子。
根据最大 LineValue 和最小 LineValue 的 RowIndex 获取 RowIndex 的最佳方法是什么
我已经这样做了作为测试,但想看看是否有更好的方法。
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
谢谢:)
一个简单的方法是使用 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
您可以使用 Lambda 表达式:
先用Max()
和Min()
求LineValue
的最大值\最小值
使用FindIndex()
查找所需值的索引
Private Function getMaxValueIndex() As Integer
Dim maxValue As Integer = Test.Max(Function(t) t.LineValue)
Dim maxValueIndex As Integer = Test.FindIndex(Function(t) t.LineValue = maxValue)
Return maxValueIndex
End Function
Private Function getMinValueIndex() As Integer
Dim minValue As Integer = Test.Min(Function(t) t.LineValue)
Dim minValueIndex As Integer = Test.FindIndex(Function(t) t.LineValue = minValue)
Return minValueIndex
End Function
如何从此示例中获取最小值和最大值
Public Class RowsFound
Property RowIndex As integer
Property Qty As Integer
Property LineValue As Double
End Class
Dim Test as new List(Of RowsFound)
上面你可以看到我列表的结构。这就是数据的样子。
根据最大 LineValue 和最小 LineValue 的 RowIndex 获取 RowIndex 的最佳方法是什么
我已经这样做了作为测试,但想看看是否有更好的方法。
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
谢谢:)
一个简单的方法是使用 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
您可以使用 Lambda 表达式:
先用
Max()
和Min()
求使用
查找所需值的索引FindIndex()
Private Function getMaxValueIndex() As Integer Dim maxValue As Integer = Test.Max(Function(t) t.LineValue) Dim maxValueIndex As Integer = Test.FindIndex(Function(t) t.LineValue = maxValue) Return maxValueIndex End Function Private Function getMinValueIndex() As Integer Dim minValue As Integer = Test.Min(Function(t) t.LineValue) Dim minValueIndex As Integer = Test.FindIndex(Function(t) t.LineValue = minValue) Return minValueIndex End Function
LineValue
的最大值\最小值