Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
All-Star
118619 Points
18779 Posts
Aug 11, 2011 02:11 AM|LINK
digish how to find the average of columns ina datatable. The columns are string. but the content is numerical
Another way is to use Average——the exnteded method of LINQ——
public class MainTest { static void Main(string[] args) { DataTable dt = new DataTable(); dt.Columns.Add("number", typeof(string)); //Add random numbers there Random r = new Random(DateTime.Now.Millisecond); for (int i = 1; i < 11; i++) { dt.Rows.Add((r.NextDouble() * 10 + 10).ToString()); } //Make a convert cast now var result = dt.AsEnumerable().Average((row)=>Convert.ToDouble(row["number"])); Console.WriteLine(result); } }
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: how to find the average of columns ina datatable
Aug 11, 2011 02:11 AM|LINK
Another way is to use Average——the exnteded method of LINQ——