newman
newman

Reputation: 6911

How to do this simple weighted average in LINQ?

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 ...

Upvotes: 8

Views: 1508

Answers (1)

dlev
dlev

Reputation: 48596

double average = values.Aggregate((x, y) => (x + y) / 2.0);

Upvotes: 9

Related Questions