Chalumeau
Chalumeau

Reputation: 101

Sum in Linq to Object query vb.net

I have a DataTable with this information :

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

The column "Repere" and "Position" are String.
The column "Prix" as Double.

I want to sum the values in the column "Prix" after grouping by "Repere" and "Position".
I improve to use a linq to object query but I don't know how I have to do to make the sum.

I give you the query that I made :

    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"))}  

Upvotes: 2

Views: 1291

Answers (1)

user2480047
user2480047

Reputation:

Here you have a code delivering what you want:

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))}

Upvotes: 4

Related Questions