vb.net 列表集合中的最小值和最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39318972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:03:07  来源:igfitidea点击:

Min and max from list collection

vb.netienumerable

提问by Etienne

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. enter image description here

上面你可以看到我的列表的结构。这就是数据的样子。 在此处输入图片说明

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

根据最大 LineValue 和 Min LineValue 的 RowIndex 获取 RowIndex 的最佳方法是什么

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 :)

谢谢 :)

采纳答案by в?a???? в?н???

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

一个简单的方法是使用 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

回答by jonathana

you can use Lambda Expressions:

您可以使用 Lambda 表达式:

  1. first find the maximum \ Minimum value of LineValueusing Max()and Min()

  2. find the index of the desired value using 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
    
  1. 首先找到LineValue使用Max()和 的最大值\最小值Min()

  2. 使用找到所需值的索引 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