C# 使用 LINQ 获取带有自定义对象的列表的总和/平均值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1136013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:04:19  来源:igfitidea点击:

Using LINQ to Get Sum/ Average of a List with custom objects

c#linq

提问by Graviton

I have a class:

我有一堂课:

public class PointD
{
    public double X
    { get; set; }
    public double Y
    { get; set; }

    public PointD(double x, double y)
    {
        X = x;
        Y=y;
    }
   //operator for +,-, * and / are overridden
 }

Given a list<PointD>, how to get the average of it using LINQ? A forloop equivalent will be something like this:

给定 a list<PointD>,如何使用 LINQ 获得它的平均值?一个for循环等效将是这样的:

  double xx, yy;
  for ( int i=0; i< ptlist.Count; i++)
     {
         xx+=ptlist[i].X;
         yy+=ptlist[i].Y; 
     }
  return new PointD(){X=xx, Y=yy};

You can use any built-inLINQ function only. You can't define an extension that takes care of this function.

您只能使用任何内置LINQ 函数。您无法定义负责此功能的扩展。

Any idea?

任何的想法?

Edit: Of course, you can use two separate Sumextension method to Sum for X and Y component before merging them. But this is not what I want. What I want is a single query/ method that does the job

编辑:当然,您可以Sum在合并之前使用两个单独的扩展方法对 X 和 Y 组件进行求和。但这不是我想要的。我想要的是完成这项工作的单个查询/方法

采纳答案by Noldorin

The Aggregatefunction would come in handy here.

这个Aggregate函数在这里会派上用场。

var sum = list.Aggregate((acc, cur) => acc + cur);
var average = list.Aggregate((acc, cur) => acc + cur) / list.Count;

Just insure that you have the /operator defined for types PointDand int, and this should do the job.

只要确保您/为类型PointD和定义了运算符int,这应该可以完成工作。

Note: I'm not quite sure whether you want the sum or average here (your question is somewhat ambiguous about this), but I've included examples for both.

注意:我不太确定您是想要总和还是平均值(您的问题对此有些模棱两可),但我已经包含了两者的示例。

回答by Greg Beech

You'll need to use the Aggregatemethod instead so you can provide your own aggregation function (Sumis just a convenient specialized case of this method). Something like:

您需要改用该Aggregate方法,以便您可以提供自己的聚合函数(Sum只是此方法的一个方便的特例)。就像是:

points.Aggregate(new PointD(0, 0), (a, p) => a + p);

I know you say you don't want any additional methods defined, but if this is a common operation I'd be inclined to write an extension method for this, i.e.

我知道你说你不想定义任何额外的方法,但如果这是一个常见的操作,我倾向于为此编写一个扩展方法,即

public static PointD Sum(this IEnumerable<PointD> source)
{
    return source.Aggregate(new PointD(0, 0), (a, p) => a + p);
} 

Because it's much more readable to be able to write:

因为能够编写更具可读性:

points.Sum();

回答by Ruben Bartelink

    [Fact]
    public void CanAddManyPointDs()
    {
        var points = new[]{
            new PointD( 1,1),
            new PointD( 2,3),
            new PointD( 3,4),
        };

        var result = points.Aggregate((p1, p2) => new PointD(p1.X + p2.X, p1.Y + p2.Y));
        Assert.Equal(result.X,6);
        Assert.Equal(result.Y,8);
    }