I am very new to LINQ to Objects so the syntax is throwing me.

Basically after I run a LINQ command which SUMs up some data, I want
to loop through and process each item returned from the query.

A subset of my code is below (The entire code list will also be
attached lower)

‘’Code Snip Start
Dim pointAssignments = GetPointAssignments()

‘This works fine
Dim totalpas = From pa In pointAssignments _
                            Group pa By pa.VehicleOID Into Group _
                            Select VehicleOID, TotalPoints = Group.Sum
(Function(pa) pa.DriverPointsAtFaultAccidents)

'I assume somehow have to convert each object in totalpas to the
TotalPontsAssignment class
'Maybe something like this, but I know I am way off
Dim tpas As List(Of TotalPointAssignment) = totalpas.Select(Function
(tpa) tpa.VehicleOID).ToList.Select(Function(tpa)
tpa.TotalPoints).ToList()

‘Note the above line is just my assumption. Maybe, I do not have to do
this.

'This is my main goal
For Each tpa As TotalPointAssignment In tpas

      'Use tpa.VehicleOID
      'Use tpa.TotalPoints

Next

‘’Code Snip End

Basically, I want to loop through totalpas and access the values
returned (VehicleOID, TotalPoints)

I assume I have to convert totalpas to something I can work with.
But, if I do not have to, that is fine as well. I just want to loop
through each item and process the data of each item.

See all of the code below:

    Public Class PointAssignment
        Private _operatorOID As Long
        Private _vehicleOID As Long
        Private _percentageOfVehicleUse As Long
        Private _vehicleRank As Long
        Private _driverPointsAtFaultAccidents As Long

        Public Property OperatorOID() As Long
            Get
                Return _operatorOID
            End Get
            Set(ByVal value As Long)
                _operatorOID = value
            End Set
        End Property

        Public Property VehicleOID() As Long
            Get
                Return _vehicleOID
            End Get
            Set(ByVal value As Long)
                _vehicleOID = value
            End Set
        End Property

        Public Property PercentageOfVehicleUse() As Long
            Get
                Return _percentageOfVehicleUse
            End Get
            Set(ByVal value As Long)
                _percentageOfVehicleUse = value
            End Set
        End Property

        Public Property VehicleRank() As Long
            Get
                Return _vehicleRank
            End Get
            Set(ByVal value As Long)
                _vehicleRank = value
            End Set
        End Property

        Public Property DriverPointsAtFaultAccidents() As Long
            Get
                Return _driverPointsAtFaultAccidents
            End Get
            Set(ByVal value As Long)
                _driverPointsAtFaultAccidents = value
            End Set
        End Property
    End Class

    Private Class TotalPointAssignment
        Private _vehicleOID As Long
        Private _totalPoints As Long

        Public Property VehicleOID() As Long
            Get
                Return _vehicleOID
            End Get
            Set(ByVal value As Long)
                _vehicleOID = value
            End Set
        End Property

        Public Property TotalPoints() As Long
            Get
                Return _totalPoints
            End Get
            Set(ByVal value As Long)
                _totalPoints = value
            End Set
        End Property
    End Class

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)

        Dim pointAssignments = GetPointAssignments()

        Dim totalpas = From pa In pointAssignments _
                            Group pa By pa.VehicleOID Into Group _
                            Select VehicleOID, TotalPoints = Group.Sum
(Function(pa) pa.DriverPointsAtFaultAccidents)

        'I assume somehow convert each object in totalpas to the
TotalPontsAssignment class
        'Maybe something like this, but I know I am way off
        Dim tpas As List(Of TotalPointAssignment) = totalpas.Select
(Function(tpa) tpa.VehicleOID).ToList.Select(Function(tpa)
tpa.TotalPoints).ToList()

        For Each tpa As TotalPointAssignment In tpas

            'Use tpa.VehicleOID
            'Use tpa.TotalPoints

        Next

        Me.GridView1.DataSource = totalpas

        Me.GridView1.DataBind()
    End Sub

    Public Function GetPointAssignments() As List(Of PointAssignment)

        Dim pointAssignments As PointAssignment() = { _
            New PointAssignment With {.OperatorOID = 1, .VehicleOID =
1, .PercentageOfVehicleUse = 90, .VehicleRank =
1, .DriverPointsAtFaultAccidents = 2}, _
            New PointAssignment With {.OperatorOID = 2, .VehicleOID =
1, .PercentageOfVehicleUse = 10, .VehicleRank =
1, .DriverPointsAtFaultAccidents = 1}, _
            New PointAssignment With {.OperatorOID = 3, .VehicleOID =
2, .PercentageOfVehicleUse = 90, .VehicleRank =
2, .DriverPointsAtFaultAccidents = 4}, _
            New PointAssignment With {.OperatorOID = 2, .VehicleOID =
2, .PercentageOfVehicleUse = 10, .VehicleRank =
2, .DriverPointsAtFaultAccidents = 1} _
            }

        Return New List(Of PointAssignment)(pointAssignments)
    End Function

Thanks for any help

Reply via email to