title: Generic LINQ extension for calculating weighted average in Visual Basic 2010
body: <p>I wrote the following generic LINQ extension for calculating weighted average in Visual Basic 2010:</p>
  
  <pre><code>&lt;Extension()&gt;
  Function WeightedAverage(Of T)(ByVal source As IEnumerable(Of T),
                ByVal selectorValue As Func(Of T, Integer),
                ByVal selectorWeight As Func(Of T, Integer)) As Double
    Dim weightedValueSum As Double
    Dim weightSum As Integer
  
    weightedValueSum = (From element In source
                        Select (selectorValue(element) * selectorWeight(element))).Sum()
    weightSum = (From element In source
                 Select selectorWeight(element)).Sum()
    If weightSum &lt;&gt; 0 Then
        Return weightedValueSum / weightSum
    Else
        Return 0
    End If
  End Function
  </code></pre>
  
  <p>How can I call this function as an Aggregate function of another LINQ query?</p>
  
  <p>I tried it in the following way:</p>
  
  <pre><code>Dim q1 = From jd In oContext.JobDatas
           Where jd.Year = 2011
           Select jd
  Dim q2 = Aggregate num In q1 Into WeightedAverage(num.AvSalary, num.NumPosHolder)
  </code></pre>
  
  <p>The Visual Basic 2010 editor is telling me that the second query (q2) of the following code is not valid statement. On the comma between the first an second argument it's saying: ")" required. What's wrong?</p>