Dim oDt As New DataTable oDt.Columns.Add("Sight", GetType(String)) oDt.Columns.Add("Quality", GetType(String)) oDt.Columns.Add("ValueBox", GetType(Double)) oDt.Columns.Add("Yield", GetType(Double)) oDt.Columns.Add("RoughStock", GetType(Double)) oDt.Columns.Add("PolishedStock", GetType(Double)) oDt.Columns.Add("InProduction", GetType(Double)) oDt.Columns.Add("Overhead", GetType(Double)) oDt.Columns.Add("Produced", GetType(Double))snip
q = From record In oDt Group By sight = record("Sight") Into totValueBox = Sum(record.Field(Of Double)("ValueBox")) _ , totYield = Sum(record.Field(Of Double)("Yield")) _ , totRoughStock = Sum(record.Field(Of Double)("RoughStock")) _ , totPolishedStock = Sum(record.Field(Of Double)("PolishedStock")) _ , totInProduction = Sum(record.Field(Of Double)("InProduction")) _ , totOverhead = Sum(record.Field(Of Double)("Overhead")) _ , totProduced = Sum(record.Field(Of Double)("Produced")) Order By sightwhereas what you had in mind was :
select sight, sum(ValueBox) as totalValueBox ... from (oDT) group by sight.And good luck if you need to group by multiple fields :
Public Sub TestLinqMultipleColumns() Dim o As New DataTable o.Columns.Add("Sight", GetType(String)) o.Columns.Add("Account", GetType(String)) o.Columns.Add("Amount", GetType(Double)) o.Columns.Add("Amount1", GetType(Double)) o.Rows.Add("S1", "A1", 1, 2) o.Rows.Add("S2", "A1", 2, 4) o.Rows.Add("S1", "A2", 3, 6) o.Rows.Add("S2", "A1", 4, 8) o.Rows.Add("S1", "A1", 5, 10) o.Rows.Add("S2", "A1", 6, 12) Dim q q = From record In o _ Group By sightAndAccount = New With {Key .Sight = record("Sight"), Key .Account = record("Account")} Into totAmount = Sum(record.Field(Of Double)("Amount")) _ , totAmount1 = Sum(record.Field(Of Double)("Amount1")) Order By sightAndAccount.Account, sightAndAccount.Account Select sightAndAccount.Sight, sightAndAccount.Account, totAmount, totAmount1 For Each o1 In q Debug.Print(o1.ToString) Next End SubThis used to be
select sight, account, sum(Amount) as totAmount,sum(Amount1) as totAmount1 group by Sight,Account"for the rest of us". And frankly, how intellisense is of any use here is beyond me.