c# LINQ Query: average value after group
Say I have a datatable with a number of columns.
I want to group the date column and for each date, I want to work out the average value of the result column.
I have the following code which is not working as expected:
var results = from res in dt.AsEnumerable()
group res by res.Field<string>("operation_time")
into grp
orderby grp.Key
select new
开发者_开发百科 {
date = grp.Key,
sum = grp.Average(r => r.Field<double>("result"))
};
Could somebody advise how I can do this?
You're sorting by the operation_time
field as a string, which is unlikely to be what you want.
You should parse it into an int
or DateTime
using their respective Parse
methods.
精彩评论