Quantcast
Channel: Telerik Forums RSS
Viewing all articles
Browse latest Browse all 94857

Weighted Average

$
0
0

Hi Kalin,

 Thanks for your response. However, this is not really what I am looking for. I need to perform a weighted average in terms of the aggregation itself. Not as a post-step calculation. I also need to select the item by which to weigh it.

 As an example, this could be achieved in SQL like so:

selectcategory, sum(item * weightItem) / sum(weightItem) asweightedAverage
fromtableName
groupbycategory

In C# it could be implemented as an extension method. E.g.:

publicstaticdoubleWeightedAverage<T>(thisIEnumerable<T> source, Func<T, double> itemSelector, Func<T, double> weightSelector)
{
    var result = source.Aggregate(Tuple.Create(0.0, 0.0), (a, t) => Tuple.Create(a.Item1 + (itemSelector(t) * weightSelector(t)), a.Item2 + weightSelector(t)));
 
    returnresult.Item1 / result.Item2;
}

Is there any way to somehow plug in this custom aggregation? 

Also worth pointing out, I'm using a QueryableDataProvider even though the data I'm working against is an IEnumerable (I'm simply performing an AsQueryable). The LocalDataSourceProvider did not perform very well when I first tried it (it loads everything into memory).

Thanks,

Charles

 


Viewing all articles
Browse latest Browse all 94857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>