Home > DeveloperSection > Forums > How to get max and min value of columns in data table?
James Smith

Total Post:48

Posted on    July-23-2011 4:42 AM

 C# C# 

 2 Reply(s)
 26147  View(s)
Rate this:


I have a small proble while making program. I want to get maximum and minimum value of column say smarks of data table. How can I perform this task as it is prity simpple in sql query I just call max and min function. Is something like it present in ADO.NET.

Help me!


Awadhendra Tiwari

Total Post:126

Posted on    July-23-2011 5:47 AM

Hi james,

You can use concept of Linq to solve your prtoblem.
Here I give you some code piece which might be solve your problem.

DataTable parentTable = ds.Tables[0];
List<int> values = parentTable.AsEnumerable().Select(age => age.Field<int>("sage")).Distinct().ToList();
string str = "Maximum Value : " + values.Max() + "  :  Minimum Value  :  " + values.Min();



Alex R
Alex R

Total Post:40

Posted on    July-23-2011 12:54 PM

Hi James,

You can do it by using compute method of DataTable.

DataTable dt = ds.Tables[0];
int min = dt.Compute("min(age)", string.Empty);
int max = dt.Compute(max(age)", string.Empty);

Hope this will solve your problem.

Don't want to miss updates? Please click the below button!

Follow MindStick