C# Linq 平均

发布于 2024-10-21 22:55:43 字数 415 浏览 2 评论 0原文

我有一个包含类似于下面的数据的表:

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

我想投影一个表,如下所示:

Group    TimePoint   AverageValue
  1          0            2
  1          1            4

编辑:数据位于数据表中。

有人知道如何使用 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

EDIT: The data is in a datatable.

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

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

裂开嘴轻声笑有多痛 2024-10-28 22:55:43

您需要执行 Group By

您需要的 linq 类似于:

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

You need to perform Group By

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)
            } ;

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

绾颜 2024-10-28 22:55:43

这是一种更注重功能的方法(我更喜欢这种方法)。第一行无法编译,因此请用您的数据填充它。

var items = new[] { new { Group = 1, TimePoint = 0, Value = 1} ... };
var answer = items.GroupBy(x => new { TimePoint = x.TimePoint, Group = x.Group })
                  .Select(x => new { 
                                     Group = x.Key.Group,
                                     TimePoint = x.Key.TimePoint,
                                     AverageValue = x.Average(y => y.Value),
                                   }
                  );

Here's a more function-oriented approach (the way I prefer it). The first line won't compile, so fill it in with your data instead.

var items = new[] { new { Group = 1, TimePoint = 0, Value = 1} ... };
var answer = items.GroupBy(x => new { TimePoint = x.TimePoint, Group = x.Group })
                  .Select(x => new { 
                                     Group = x.Key.Group,
                                     TimePoint = x.Key.TimePoint,
                                     AverageValue = x.Average(y => y.Value),
                                   }
                  );
沉睡月亮 2024-10-28 22:55:43

你可以这样做:

IEnumerable<MyClass> table = ...

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

You can do:

IEnumerable<MyClass> table = ...

var query = from item in table
            group item by new { item.Group, item.TimePoint } into g
            select new
            {
                g.Key.Group,
                g.Key.TimePoint,
                AverageValue = g.Average(i => i.Value)
            };
情绪少女 2024-10-28 22:55:43

假设有这样一个类:

public class Record
{
  public int Group {get;set;}
  public int TimePoint {get;set;}
  public int Value {get;set;}
}

var groupAverage = from r in records
                   group r by new { r.Group, r.TimePoint } into groups
                   select new
                          {
                            Group = groups.Key.Group,
                            TimePoint = groups.Key.TimePoint,
                            AverageValue = groups.Average(rec => rec.Value)
                          };

Assuming a class like this:

public class Record
{
  public int Group {get;set;}
  public int TimePoint {get;set;}
  public int Value {get;set;}
}

var groupAverage = from r in records
                   group r by new { r.Group, r.TimePoint } into groups
                   select new
                          {
                            Group = groups.Key.Group,
                            TimePoint = groups.Key.TimePoint,
                            AverageValue = groups.Average(rec => rec.Value)
                          };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文