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 for
loop equivalent will be something like this:
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-in LINQ function only. You can't define an extension that takes care of this function.
Any idea?
Edit: Of course, you can use two separate Sum
extension 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