如何选择数据表中列的最小值和最大值?

时间:2022-05-12 20:14:06

For the following datatable column, what is the fastest way to get the min and max values?

对于以下数据表列,获取最小值和最大值的最快方法是什么?

AccountLevel  
0  
1  
2  
3 

11 个解决方案

#1


47  

int minAccountLevel = int.MaxValue;
int maxAccountLevel = int.MinValue;
foreach (DataRow dr in table.Rows)
{
    int accountLevel = dr.Field<int>("AccountLevel");
    minAccountLevel = Math.Min(minAccountLevel, accountLevel);
    maxAccountLevel = Math.Max(maxAccountLevel, accountLevel);
}

Yes, this really is the fastest way. Using the Linq Min and Max extensions will always be slower because you have to iterate twice. You could potentially use Linq Aggregate, but the syntax isn't going to be much prettier than this already is.

是的,这确实是最快的方式。使用Linq Min和Max扩展名将总是较慢,因为您必须迭代两次。您可能会使用Linq Aggregate,但语法不会比现在更漂亮。

#2


87  

Easiar approach on datatable could be:

对数据表的Easiar方法可能是:

int minLavel = Convert.ToInt32(dt.Compute("min([AccountLevel])", string.Empty));

#3


12  

Use LINQ. It works just fine on datatables, as long as you convert the rows collection to an IEnumerable.

使用LINQ。只要将rows集合转换为IEnumerable,它就可以在数据表上正常工作。

List<int> levels = AccountTable.AsEnumerable().Select(al => al.Field<int>("AccountLevel")).Distinct().ToList();
int min = levels.Min();
int max = levels.Max();

Edited to fix syntax; it's tricky when using LINQ on DataTables, and aggregating functions are fun, too.

编辑修复语法;在DataTables上使用LINQ时很棘手,聚合函数也很有趣。

Yes, it can be done with one query, but you will need to generate a list of results, then use .Min() and .Max() as aggregating functions in separate statements.

是的,可以使用一个查询完成,但是您需要生成结果列表,然后使用.Min()和.Max()作为单独语句中的聚合函数。

#4


6  

The most efficient way to do this (believe it or not) is to make two variables and write a for loop.

执行此操作(信不信由你)的最有效方法是创建两个变量并编写for循环。

#5


5  

var answer = accountTable.Aggregate(new { Min = int.MinValue, Max = int.MaxValue }, 
                                        (a, b) => new { Min = Math.Min(a.Min, b.Field<int>("AccountLevel")),
                                                        Max = Math.Max(a.Max, b.Field<int>("AccountLevel")) });
int min = answer.Min;
int max = answer.Max;

1 iteration, linq style :)

1次迭代,linq风格:)

#6


5  

This worked fine for me

这对我来说很好

int  max = Convert.ToInt32(datatable_name.AsEnumerable()
                        .Max(row => row["column_Name"]));

#7


4  

another way of doing this is

另一种方法是这样做

int minLavel = Convert.ToInt32(dt.Select("AccountLevel=min(AccountLevel)")[0][0]);

I am not sure on the performace part but this does give the correct output

我不确定在性能部分,但这确实给出了正确的输出

#8


1  

var min = dt.AsEnumerable().Min(row => row["AccountLevel"]);
var max = dt.AsEnumerable().Max(row => row["AccountLevel"]);

#9


1  

Performance wise, this should be comparable. Use Select statement and Sort to get a list and then pick the first or last (depending on your sort order).

性能方面,这应该具有可比性。使用Select语句和Sort来获取列表,然后选择第一个或最后一个(取决于您的排序顺序)。

var col = dt.Select("AccountLevel", "AccountLevel ASC");

var min = col.First();
var max = col.Last();

#10


1  

Session["MinDate"] = dtRecord.Compute("Min(AccountLevel)", string.Empty);
Session["MaxDate"] = dtRecord.Compute("Max(AccountLevel)", string.Empty);

#11


0  

I don't know how my solution compares performance wise to previous answers.

我不知道我的解决方案如何将性能与先前的答案进行比较。

I understand that the initial question was: What is the fastest way to get min and max values in a DataTable object, this may be one way of doing it:

我知道最初的问题是:在DataTable对象中获取最小值和最大值的最快方法是什么,这可能是一种方法:

DataView view = table.DefaultView;
view.Sort = "AccountLevel";
DataTable sortedTable = view.ToTable();
int min = sortedTable.Rows[0].Field<int>("AccountLevel");
int max = sortedTable.Rows[sortedTable.Rows.Count-1].Field<int>("AccountLevel");

It's an easy way of achieving the same result without looping. But performance will need to be compared with previous answers. Thought I love Cylon Cats answer most.

这是一种在没有循环的情况下实现相同结果的简单方法。但是需要将性能与之前的答案进行比较。以为我爱Cylon Cats的回答最多。

#1


47  

int minAccountLevel = int.MaxValue;
int maxAccountLevel = int.MinValue;
foreach (DataRow dr in table.Rows)
{
    int accountLevel = dr.Field<int>("AccountLevel");
    minAccountLevel = Math.Min(minAccountLevel, accountLevel);
    maxAccountLevel = Math.Max(maxAccountLevel, accountLevel);
}

Yes, this really is the fastest way. Using the Linq Min and Max extensions will always be slower because you have to iterate twice. You could potentially use Linq Aggregate, but the syntax isn't going to be much prettier than this already is.

是的,这确实是最快的方式。使用Linq Min和Max扩展名将总是较慢,因为您必须迭代两次。您可能会使用Linq Aggregate,但语法不会比现在更漂亮。

#2


87  

Easiar approach on datatable could be:

对数据表的Easiar方法可能是:

int minLavel = Convert.ToInt32(dt.Compute("min([AccountLevel])", string.Empty));

#3


12  

Use LINQ. It works just fine on datatables, as long as you convert the rows collection to an IEnumerable.

使用LINQ。只要将rows集合转换为IEnumerable,它就可以在数据表上正常工作。

List<int> levels = AccountTable.AsEnumerable().Select(al => al.Field<int>("AccountLevel")).Distinct().ToList();
int min = levels.Min();
int max = levels.Max();

Edited to fix syntax; it's tricky when using LINQ on DataTables, and aggregating functions are fun, too.

编辑修复语法;在DataTables上使用LINQ时很棘手,聚合函数也很有趣。

Yes, it can be done with one query, but you will need to generate a list of results, then use .Min() and .Max() as aggregating functions in separate statements.

是的,可以使用一个查询完成,但是您需要生成结果列表,然后使用.Min()和.Max()作为单独语句中的聚合函数。

#4


6  

The most efficient way to do this (believe it or not) is to make two variables and write a for loop.

执行此操作(信不信由你)的最有效方法是创建两个变量并编写for循环。

#5


5  

var answer = accountTable.Aggregate(new { Min = int.MinValue, Max = int.MaxValue }, 
                                        (a, b) => new { Min = Math.Min(a.Min, b.Field<int>("AccountLevel")),
                                                        Max = Math.Max(a.Max, b.Field<int>("AccountLevel")) });
int min = answer.Min;
int max = answer.Max;

1 iteration, linq style :)

1次迭代,linq风格:)

#6


5  

This worked fine for me

这对我来说很好

int  max = Convert.ToInt32(datatable_name.AsEnumerable()
                        .Max(row => row["column_Name"]));

#7


4  

another way of doing this is

另一种方法是这样做

int minLavel = Convert.ToInt32(dt.Select("AccountLevel=min(AccountLevel)")[0][0]);

I am not sure on the performace part but this does give the correct output

我不确定在性能部分,但这确实给出了正确的输出

#8


1  

var min = dt.AsEnumerable().Min(row => row["AccountLevel"]);
var max = dt.AsEnumerable().Max(row => row["AccountLevel"]);

#9


1  

Performance wise, this should be comparable. Use Select statement and Sort to get a list and then pick the first or last (depending on your sort order).

性能方面,这应该具有可比性。使用Select语句和Sort来获取列表,然后选择第一个或最后一个(取决于您的排序顺序)。

var col = dt.Select("AccountLevel", "AccountLevel ASC");

var min = col.First();
var max = col.Last();

#10


1  

Session["MinDate"] = dtRecord.Compute("Min(AccountLevel)", string.Empty);
Session["MaxDate"] = dtRecord.Compute("Max(AccountLevel)", string.Empty);

#11


0  

I don't know how my solution compares performance wise to previous answers.

我不知道我的解决方案如何将性能与先前的答案进行比较。

I understand that the initial question was: What is the fastest way to get min and max values in a DataTable object, this may be one way of doing it:

我知道最初的问题是:在DataTable对象中获取最小值和最大值的最快方法是什么,这可能是一种方法:

DataView view = table.DefaultView;
view.Sort = "AccountLevel";
DataTable sortedTable = view.ToTable();
int min = sortedTable.Rows[0].Field<int>("AccountLevel");
int max = sortedTable.Rows[sortedTable.Rows.Count-1].Field<int>("AccountLevel");

It's an easy way of achieving the same result without looping. But performance will need to be compared with previous answers. Thought I love Cylon Cats answer most.

这是一种在没有循环的情况下实现相同结果的简单方法。但是需要将性能与之前的答案进行比较。以为我爱Cylon Cats的回答最多。