6

I'm just curious if this could be done in a single LINQ statement. I need to do a simple weighted average like this:

IEnumerable<double> values = { v0, v1, v2, v3, ...}
WeightedAverage = (((v0 + v1) / 2 + v2) / 2 + v3) / 2 ...
newman
  • 6,841
  • 21
  • 79
  • 126
  • possible duplicate of [Weighted Average with LINQ](http://stackoverflow.com/questions/2714639/weighted-average-with-linq) – Dmitry Pavlov Mar 12 '14 at 11:29

1 Answers1

7
double average = values.Aggregate((x, y) => (x + y) / 2.0);
dlev
  • 48,024
  • 5
  • 125
  • 132