Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Stored procedure is returning set of columns of data like name, age and height. At the application level, in c# how can we apply the aggregate functions to get average height, min age and max age on the data.

Actually i can have any stored procedure getting executed at this point with varied columns coming out, on top of that data table i might have to apply the aggregate functions. Please suggest

What I have tried:

I know the way to handle it if i have to execute only one proc, but at this point i am not aware what would be the return columns for my proc
Posted
Updated 14-Mar-17 22:30pm

1 solution

DataTable dt = new DataTable();
      dt.Columns.Add("name");
      dt.Columns.Add("age");
      dt.Columns.Add("height");
      dt.Rows.Add("AA", 25, 5.2);
      dt.Rows.Add("bb", 26, 4.2);
      dt.Rows.Add("cc", 28, 6.2);
      dt.Rows.Add("dd", 30, 7);
      var average = dt.Rows.OfType<DataRow>().Select(k => Convert.ToDouble(k["height"])).Average();
      var ages = dt.Rows.OfType<DataRow>().Select(k => Convert.ToInt32(k["age"]));
      int minage = ages.Min();
      int max = ages.Max();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900