我有一个类:
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
}
如何使用LINQ获取list<PointD>
的平均值?等价于使用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};
您只能使用任何内置的LINQ函数。 您不能定义一个扩展来处理此函数。
有什么想法吗?
编辑:当然,您可以在合并它们之前使用两个单独的Sum
扩展方法对X和Y分量进行求和。 但这不是我想要的。 我想要的是一个单一的查询/方法来完成工作