Linq 多列分组和平均
我试图在分组后获得一些数字的平均值,但它不会为我分组。为什么这没有做我认为应该做的事情?
var eventInfo = from eventData in dt.AsEnumerable()
group eventData by new
{
Phase1Minutes = eventData.Field<decimal>("Phase1Minutes"),
Phase2Minutes = eventData.Field<decimal>("Phase2Minutes"),
TechnologyType = eventData.Field<string>("TechnologyType"),
TechnologySubType = eventData.Field<string>("TechnologySubType")
} into g
select new
{
Phase1Avg = g.Average(x => x.Field<decimal>("Phase1Minutes")),
Phase2Avg = g.Average(x => x.Field<decimal>("Phase2Minutes")),
g.Key.TechnologyType,
g.Key.TechnologySubType
};
I'm trying to get the average of some numbers after grouping but it just won't group for me. Why isn't this doing what I think it should?
var eventInfo = from eventData in dt.AsEnumerable()
group eventData by new
{
Phase1Minutes = eventData.Field<decimal>("Phase1Minutes"),
Phase2Minutes = eventData.Field<decimal>("Phase2Minutes"),
TechnologyType = eventData.Field<string>("TechnologyType"),
TechnologySubType = eventData.Field<string>("TechnologySubType")
} into g
select new
{
Phase1Avg = g.Average(x => x.Field<decimal>("Phase1Minutes")),
Phase2Avg = g.Average(x => x.Field<decimal>("Phase2Minutes")),
g.Key.TechnologyType,
g.Key.TechnologySubType
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对已经分组的内容进行平均是没有意义的。举一个简单的例子,想象一下按年龄对人们进行分组,然后找到每组中的平均年龄......他们都是相同的年龄!
我的猜测是你只需要去掉分组的分钟部分:
It makes no sense to average over what you're already grouping by. Just to give a trivial example, imagine grouping people by age, and then finding the average age within each group... they're all the same age!
My guess is you just need to take out the minutes parts of the grouping: