SUM property from list of lists using LINQ

Hobbyist_programmer 621 Reputation points
2021-04-16T10:40:14.41+00:00

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

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-04-16T10:57:13.677+00:00

    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Hobbyist_programmer 621 Reputation points
    2021-04-16T11:06:29.323+00:00

    thanks Viorel. It worked.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.