c# LINQ Query:分组后的平均值
假设我有一个包含多列的数据表。
我想对日期列进行分组,对于每个日期,我想计算出结果列的平均值。
我有以下代码,但未按预期工作:
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"))
};
有人可以建议我如何做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您按
operation_time
字段作为字符串进行排序,这不太可能是您想要的。您应该使用各自的
Parse
方法将其解析为int
或DateTime
。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
orDateTime
using their respectiveParse
methods.