开发者

c# LINQ Query: average value after group

开发者 https://www.devze.com 2023-02-14 04:32 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消