查找 VB.Net DataTable 的最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11116445/
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-09 16:09:42  来源:igfitidea点击:

Find the maximum value of a VB.Net DataTable

vb.netdatatableintegermax

提问by Matt

I have been very stumped by this one. I have a fairly large (~1500 x ~1000) DataTable of positive and negative integers onlythat I acquired from a .csv file. For my program, I need to find the maximum value of the entire table, not just in a single row or column. Optimally, the code would be short and sweet, but that's not always the case ;).

我一直被这个难住了。我有一个相当大(~1500 x ~1000)的正整数和负整数数据表,只有我从 .csv 文件中获取。对于我的程序,我需要找到整个表的最大值,而不仅仅是单行或单列。最理想的情况是,代码应该短小精悍,但情况并非总是如此 ;)。

The name of my DataTable is BeamMapand I am trying to return a value of MaxValue(already declared as an integer). I can post the code I have for creating the DataTable upon request.

我的 DataTable 的名称是BeamMap,我正在尝试返回一个值MaxValue(已声明为整数)。我可以根据要求发布用于创建 DataTable 的代码。

Extra Credit: (not really)

额外的信用:(不是真的)

Is there a way to quicklyfind the location (ie., row,column) of said maximum value? So far, all of the examples I've seen check cell by cell for a predetermined value, which is rather inefficient for the number of data points that I have.

有没有办法快速找到所述最大值的位置(即行、列)?到目前为止,我看到的所有示例都逐个单元格检查预定值,这对于我拥有的数据点数量来说效率很低。

采纳答案by Matt Wilko

This code will do it. I haven't tried with a huge datatable so you will have to see how long it takes:

这段代码会做到这一点。我还没有尝试过一个巨大的数据表,所以你必须看看它需要多长时间:

Private Function FindMaxDataTableValue(ByRef dt As DataTable) As Integer
    Dim currentValue As Integer, maxValue As Integer
    Dim dv As DataView = dt.DefaultView
    For c As Integer = 0 To dt.Columns.Count - 1
        dv.Sort = dt.Columns(c).ColumnName + " DESC"
        currentValue = CInt(dv(0).Item(c))
        If currentValue > maxValue Then maxValue = currentValue
    Next
    Return maxValue
End Function

It sorts each column in turn and if the first value is larger than the current largest value it updates it.

它依次对每一列进行排序,如果第一个值大于当前最大值,则更新它。

For the extra credit you can do this but there might be a performance hit doing the IndexOfto find the rowIndex:

对于额外的信用,您可以这样做,但可能会影响性能IndexOf以找到rowIndex

Private Function FindMaxDataTableValue(ByRef dt As DataTable) As Integer
    Dim currentValue As Integer, maxValue As Integer
    Dim rowIndex As Integer, colIndex As Integer
    Dim dv As DataView = dt.DefaultView
    For c As Integer = 0 To dt.Columns.Count - 1
        dv.Sort = dt.Columns(c).ColumnName + " DESC"
        currentValue = CInt(dv(0).Item(c))
        If currentValue > maxValue Then
            rowIndex = dt.Rows.IndexOf(dv(0).Row)
            colIndex = c
            maxValue = currentValue
        End If
    Next
    Debug.WriteLine("Max value found at Col:" + colIndex.ToString + " Row:" + rowIndex.ToString)
    Return maxValue
End Function

回答by user3549709

You can also use Compute("MAX(columnName),"") method to find maximum value on a column

您还可以使用 Compute("MAX(columnName),"") 方法来查找列上的最大值

Private Function FindMaxDataTableValue(ByRef dt As DataTable) As Integer

    Dim currentValue As Integer, maxValue As Integer
    maxValue = 0

    For c As Integer = 0 To dt.Columns.Count - 1
        currentValue = dt.Compute("MAX(c)", "")
        If currentValue > maxValue Then maxValue = currentValue
    Next
    Return maxValue

End Function

回答by Kristian

I couldn't get user3549709's code to work as kept getting "Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier." error. All my tables are filled manually and don't get their data from a database - I don't know if this makes a difference.

我无法让 user3549709 的代码正常工作,因为不断收到“聚合参数中的语法错误:期望带有可能的‘子’限定符的单列参数。” 错误。我所有的表格都是手动填写的,并且没有从数据库中获取数据 - 我不知道这是否有区别。

This is what I used instead to find the minimum and maximum values:

这是我用来查找最小值和最大值的方法:

    Dim intcurrentValue As Integer
    Dim intmaxValue As Integer
    Dim intMinValue As Integer

    intmaxValue = 0
    intMinValue = 0

    For Each colMyColumn As DataColumn In dtDelta_E.Columns
        intcurrentValue = dtDelta_E.Compute("MAX([" & colMyColumn.ColumnName & "])", "")
        If intcurrentValue > intmaxValue Then intmaxValue = intcurrentValue
        intcurrentValue = dtDelta_E.Compute("MIN([" & colMyColumn.ColumnName & "])", "")
        If intcurrentValue < intMinValue Then intMinValue = intcurrentValue
    Next

Note: You only need the square brackets [] if you have spaces or the like in your column names

注意:如果列名中有空格等,则只需要方括号 []