Linq 多列分组和平均

发布于 2024-10-16 02:15:02 字数 748 浏览 1 评论 0原文

我试图在分组后获得一些数字的平均值,但它不会为我分组。为什么这没有做我认为应该做的事情?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫﹏色ふ单纯 2024-10-23 02:15:02

对已经分组的内容进行平均是没有意义的。举一个简单的例子,想象一下按年龄对人们进行分组,然后找到每组中的平均年龄......他们都是相同的年龄!

我的猜测是你只需要去掉分组的分钟部分:

var eventInfo = from eventData in dt.AsEnumerable()
    group eventData by new
        {
            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
    };

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:

var eventInfo = from eventData in dt.AsEnumerable()
    group eventData by new
        {
            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
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文