Weighted Average with LINQ
Posted
by jsmith
on Stack Overflow
See other posts from Stack Overflow
or by jsmith
Published on 2010-04-26T15:23:39Z
Indexed on
2010/04/26
15:53 UTC
Read the original article
Hit count: 321
My goal is to get a weighted average from one table, based on another tables primary key.
Example Data:
Table1
Key WEIGHTED_AVERAGE
0200 0
Table2
ForeignKey LENGTH PCR
0200 105 52
0200 105 60
0200 105 54
0200 105 -1
0200 47 55
I need to get a weighted average based on the length of a segment and I need to ignore values of -1. I know how to do this in SQL, but my goal is to do this in LINQ. It looks something like this in SQL:
SELECT Sum(t2.PCR*t2.LENGTH)/Sum(t2.LENGTH) AS WEIGHTED_AVERAGE
FROM Table1 t1, Table2 t2
WHERE t2.PCR <> -1
AND t2.ForeinKey = t1.Key;
I am still pretty new to LINQ, and having a hard time figuring out how I would translate this. The result weighted average should come out to roughly 55.3. Thank you.
© Stack Overflow or respective owner