2013-11-27 66 views
2

我有這個信息的DataTable:總和在LINQ to對象查詢vb.net

|"Repere"|"Position"|"Prix"| 
|"AA" | "1"  | 1800 | 
|"AA" | "1"  | 5200 | 
|"AA" | "2"  | 1500 | 
|"AA" | "2"  | 0300 | 
|"BB" | "1"  | 0010 | 

列 「Repere」 和 「位置」 的字符串。
列「Prix」爲雙。

我想在「Repere」和「Position」分組之後對列「Prix」中的值進行求和。
我改進使用LINQ來對象查詢,但我不知道我怎麼做才能做出總和。

我給你,我做了查詢:

Dim ReqPrixModel = From Row In dTable_Devis.Rows _ 
         Group Row.item("Prix") By Rep = Row.Item("Repere"), Pos = Row.item("Position") Into Group _ 
         Select New With {.Prix = From i In dTable_Devis.Rows 
               Where (i.Item("Repere") = Rep And i.item("Position") = Pos) 
               Select (i.Item("Prix"))} 

回答

4

這裏有一個代碼,提供你想要的東西:

Dim ReqPrixModel = From Row In dTable_Devis _ 
       Group DirectCast(Row, DataRow).Item("Prix") By Rep = Row.Item("Repere"), Pos = Row.Item("Position") Into grouped = Group _ 
       Select New With {.Prix = grouped.Sum(Function(r) DirectCast(r, Double))} 
+0

非常感謝您Varocarbas。它的工作完全符合我的預期。 – Chalumeau

+0

@Chalumeau不客氣。 – varocarbas