2

I have DataTable object that is filled with numeric data from my database. I have to handle all possible numeric-like data types (int32, int64, float, even datetime, but that last is not necessary).

I have to normalize all data in columns, each column separately, so I need to find maximum and minimum values for each column to calculate normalization coefficient.

Do I have to iterate "manually" through all rows to find these max and min values?

Some background:

I don't want to do this in SQL, because its kind of scientific application, where user works in SQL language and writes very complicated SQL queries. I don't want to force user to write even more complicated queries to get normalized data or get min/max values

Colums are fully dynamic, they depend on SQL query written by user.

4

5 回答 5

3

我是否必须“手动”遍历所有行才能找到这些最大值和最小值?

手动定义。是的,您必须通过枚举 all来计算Min和值。但这可以用旧方法或用MaxDataRowsDataTable.Compute

林克

int minVal = table.AsEnumerable().Min(r => r.Field<int>("ColName"));
int maxVal = table.AsEnumerable().Max(r => r.Field<int>("ColName"));

DataTable.Compute

int maxVal = (int)table.Compute("Max(ColName)", "");
于 2013-04-24T13:44:09.537 回答
1

你也可以使用

Convert.ToInt32(datatable.Compute("min(columnname)", string.Empty));
Convert.ToInt32(datatable.Compute("max(columnname)", string.Empty));
于 2013-04-24T13:45:02.643 回答
1

您可以使用DataTable.Select()as 来做到这一点:

DataRow [] dr = dataTable.Select("ID= MAX(ID)");  

 if(dr !=null)
    {
     // Console.WriteLine(dr[0]["ID"]);
        int maxVal=Convert.ToInt32(dr[0]["ID"]);
    }
于 2013-04-24T13:47:30.097 回答
1

尝试这个:

var min = myTable.AsEnumerable().Min(x => (int)x["column"]);
var max = myTable.AsEnumerable().Max(x => (int)x["column"]);

您需要确保您有对 的引用System.Data.DataSetExtensions,默认情况下它不会添加到新项目中。

于 2013-04-24T13:43:23.857 回答
0

这对我来说很好

int  max = Convert.ToInt32(datatable_name.AsEnumerable()
                        .Max(row => row["column_Name"]));
于 2013-09-27T06:32:14.363 回答