C#中的LINQ平均 [英] C# Linq Average

查看:154
本文介绍了C#中的LINQ平均的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据类似下面的表格:

I have a table with data similar to below:

Group    TimePoint    Value
  1          0          1
  1          0          2
  1          0          3
  1          1          3
  1          1          5

我要树立一个表作为这样的:

I want to project a table as such:

Group    TimePoint   AverageValue
  1          0            2
  1          1            4

编辑:数据是一个DataTable

The data is in a datatable.

任何人任何想法如何能与LINQ做或以其他方式?

Anybody any ideas how this can be done with LINQ or otherwise?

感谢。

推荐答案

您需要执行集团通过

您所需要的LINQ是这样的:

The linq you need is something like:

var query = from item in inputTable
            group item by new { Group = item.Group, TimePoint = item.TimePoint } into grouped
            select new
            {
                Group = grouped.Key.Group,
                TimePoint = grouped.Key.TimePoint,
                AverageValue = grouped.Average(x => x.Value)
            } ;

有关更多LINQ的样本,我强烈推荐的101 LINQ的样本页面 - http://msdn.microsoft.com/en-us/vcsharp/aa336747#avgGrouped

For more Linq samples, I highly recommend the 101 Linq samples page - http://msdn.microsoft.com/en-us/vcsharp/aa336747#avgGrouped

这篇关于C#中的LINQ平均的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆