HOW TO GET MAX AND MIN VALUE OF COLUMNS IN DATA TABLE?

James Smith

Total Post:48

Points:336
Posted by  James Smith
C# 
C#
 26555  View(s)
Ratings:
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.

  1. Awadhendra Tiwari

    Post:126

    Points:882
    Re: How to get max and min value of columns in data table?

    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.

  1. Alex R

    Post:40

    Points:280
    Re: How to get max and min value of columns in data table?

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!