How to select minimum and maximum column values ​​in datatable?

For the next data column, what is the fastest way to get the min and max values?

AccountLevel 0 1 2 3 
+50
c # select datatable
Mar 14
source share
11 answers
 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 is really the fastest way. Using the Linq Min and Max extensions will always be slower because you need to iterate twice. You could use Linq Aggregate , but the syntax won't be much prettier than it already is.

+45
Mar 14 '10 at 15:58
source share

An easy datatable approach might be:

 int minLavel = Convert.ToInt32(dt.Compute("min([AccountLevel])", string.Empty)); 
+82
Feb 07 2018-11-11T00:
source share

Use LINQ. It works fine on datatables if you convert a collection of strings to IEnumerable.

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

Edited to correct syntax; this is difficult when using LINQ in DataTables, as well as aggregation functions too.

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

+12
Mar 14 '10 at 15:16
source share

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

+5
Mar 14 '10 at 15:05
source share
 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 :)

+5
Mar 14 '10 at 15:59
source share

It worked great for me

 int max = Convert.ToInt32(datatable_name.AsEnumerable() .Max(row => row["column_Name"])); 
+5
Sep 27 '13 at 6:37
source share

Another way to do this:

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

I'm not sure about the execution part, but it gives the right conclusion

+3
Feb 23 '12 at 13:50
source share
 var min = dt.AsEnumerable().Min(row => row["AccountLevel"]); var max = dt.AsEnumerable().Max(row => row["AccountLevel"]); 
+1
Jul 15 '15 at 7:28
source share

Efficiency, it should be comparable. Use the Select and Sort operator to get a list, and then select the first or last (depending on your sort order).

 var col = dt.Select("AccountLevel", "AccountLevel ASC"); var min = col.First(); var max = col.Last(); 
+1
Jul 17 '15 at 14:30
source share
 Session["MinDate"] = dtRecord.Compute("Min(AccountLevel)", string.Empty); Session["MaxDate"] = dtRecord.Compute("Max(AccountLevel)", string.Empty); 
+1
Feb 16 '17 at 21:15
source share

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

I understand that the initial question is: what is the fastest way to get the min and max values ​​in a DataTable, this may be one way to do this:

 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"); 

This is an easy way to achieve the same result without a loop. But performance should be comparable to previous answers. I think I love Cylon Cats to answer most.

0
Jan 19 '12 at 3:29
source share



All Articles