question

Hobbyistprogrammer-7674 avatar image
0 Votes"
Hobbyistprogrammer-7674 asked Hobbyistprogrammer-7674 answered

SUM property from list of lists using LINQ

Hallo,

I am trying to get a sum of all "Total" form the object Item. I have list called "Areas" and it has the custom object "Items", it is the list of Item. and the Item has a property "Total"

I want to sum all the total from Areas. how can i do it?

 Public Class AreaObj
     Public property Items as Items
 End class
    
 Public Class Areas
     Inherits System.ComponentModel.BindingList(Of AreaObj)
 End class
    
    
 Public Class Item
     Public property Total as integer
 End class
    
 Public Class Items
     Inherits System.ComponentModel.BindingList(Of Item)
 End class

Thanks

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered

Check this query:

 Dim areas As Areas = . . .
 Dim sum = areas.SelectMany(Function(a) a.Items).Sum(Function(i) i.Total)

It assumes that the objects are not Nothing.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hobbyistprogrammer-7674 avatar image
0 Votes"
Hobbyistprogrammer-7674 answered

thanks Viorel. It worked.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.