0

I want to understand what means this code

Dim myValue As Double = range.Sum(Function(x) x.Positive)

range is a List(Of Money). I have to mention That I have this Private Class

Private Class Money
        Private m_Positive As Double
        Private m_Negative As Double
        Public Property Positive As Double
            Get
                Return m_Positive
            End Get
            Set(value As Double)
                m_Positive = value
            End Set
        End Property
        Public Property Negative As Double
            Get
                Return m_Negative
            End Get
            Set(value As Double)
                m_Negative= value
            End Set
        End Property
    End Class
YABD
  • 5
  • 1
  • 5

1 Answers1

0

That is a Linq(to-Objects) query using Enumerable.Sum. It uses the property Positive of the class Money to sum all objects in the List(Of Money). Therefore it uses a Lambda Expression to project it.

It is similar to a loop:

Dim sum As Double = 0
For Each m As Money In range
    sum += m.Positive
Next

You could also use query syntax, which i often find more readable in VB.NET then using the ugly Function keyword:

Dim positives = From m As Money In range 
                Select m.Positive
Dim sum = positives.Sum()  ' this executes the query above '
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939