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

Total Post:48

Points:336
Posted on    July-23-2011 4:42 AM

 C# C# 
Ratings:


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

Hi...

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!

Thanks.



Awadhendra Tiwari

Total Post:126

Points:882
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();
MessageBox.Show(str);

 

Thanks.


Alex R
Alex R

Total Post:40

Points:280
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