Link to home
Start Free TrialLog in
Avatar of indy500fan
indy500fan

asked on

Get Data From a Dictionary(of String, cls) and populate the details into a listview...LINQ?

Friends,

I want to get the data details from my list (detailed in the attached code section below) and display it in a listview.

While, it sounds easy, I'm not sure how to go about it in this particular instance.  I'm guessing something like this:

    Private Sub GetDriversLapData(ByVal DriverID As String)
        'Dim lapsAny = (From lap In frmROPMain.DriverLapsList(DriverID))
        'If lapsAny.Count > 0 Then
        Dim LastLap As Integer
        Dim LastSpeed As Decimal
        For Each lap In frmROPMain.DriverLapsList(DriverID)
            Dim lapsCurrentRun = (From lap In DriverLapsList(DriverID) Where lap.RunID = My.Settings.RunID)
            If lapsCurrentRun.Count > 0 Then

                LastLap = (From lap In lapsCurrentRun Where lap.RunID = My.Settings.RunID Select lap.Lap).Max
                LastSpeed = (From lap In lapsCurrentRun Where lap.Lap = LastLap Select lap.LapSpeed).Single
                ' li.SubItems.Add(CType(LastSpeed, String))

            End If

        Next

However, this won't even compile...

Looking for VB.net syntax please.

Thanks in advance!

Eric

'Declaration of Memory Object:

Private DriverList As New Dictionary(Of String, List(Of clsLapDetail))

Adding to the list:

Dim LapDetail As New clsLapDetail
                    LapDetail.ElapsedTime = ElapsedTime
                    LapDetail.Lap = LapCount
                    LapDetail.PassingTime = PassingTime
                    LapDetail.LapTime = Laptime
                    LapDetail.LapSpeed = LapSpeed

                    SyncLock DriverList
                        If DriverList.ContainsKey(DriverID) Then
                            DriverList(DriverID).Add(LapDetail)
                        End If
                    End SyncLock

Open in new window

Avatar of MikeToole
MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

>> However, this won't even compile...
What's the message?

... I'd guess it's something to do with the double declaration of 'lap' in these two lines:
       For Each lap In frmROPMain.DriverLapsList(DriverID)
            Dim lapsCurrentRun = (From lap In DriverLapsList(DriverID) Where lap.RunID = My.Settings.RunID)

It's not obvious what you're trying to get at. Is there a RunID property in clsLapDetail? - the code you posted doesn't show it being set.

If all you want is the last instance of clsLapDetail that was added to DriverLapList for your DriverID then all that is needed is:

dim lap = DriverLapsList(DriverID).Last()

Avatar of indy500fan
indy500fan

ASKER

Here's what I came up with:

And it works...

SyncLock frmROPMain.DriverLapsList
            Dim lapsCurrentRun = (From lap In frmROPMain.DriverLapsList(DriverID) Where lap.RunID = My.Settings.RunID)
            If lapsCurrentRun.Count > 0 Then
                Dim i As Integer = lapsCurrentRun.Count
                If LastLapCount <> i Then
                    For value As Integer = 0 To i - 1
                        'Dim LapNumber As Integer = (From laps In lapsCurrentRun Where laps.RunID = My.Settings.RunID Select laps.RunID).ElementAt(value)
                        Dim intLapNumber As Integer = (From laps In lapsCurrentRun Where laps.RunID = My.Settings.RunID Select laps.Lap).ElementAt(value)
                        'Dim intLapTime As Integer = (From laps In lapsCurrentRun Where laps.RunID = My.Settings.RunID Select laps.LapTime).ElementAt(value)
                        'Dim strLapTime As String = FormatTime.GetFullyFormattedTime_Decimal(CType(intLapTime * 0.0001, String))

                        Dim intPassingTime As Integer = (From laps In lapsCurrentRun Where laps.RunID = My.Settings.RunID Select laps.PassingTime).ElementAt(value)
                        Dim strPassingTime As String = FormatTime.GetFullyFormattedTime_Integer(CType(intPassingTime * 0.0001, String))

                        Dim intElapsedTime As Integer = (From laps In lapsCurrentRun Where laps.RunID = My.Settings.RunID Select laps.ElapsedTime).ElementAt(value)
                        Dim strElapsedTime As String = FormatTime.GetFullyFormattedTime_Integer(CType(intElapsedTime * 0.0001, String))

                        Dim decLapSpeed As Decimal = (From laps In lapsCurrentRun Where laps.RunID = My.Settings.RunID Select laps.LapSpeed).ElementAt(value)
                        li = dblvROPResults.Items.Add(intLapNumber)
                        li.SubItems.Add(decLapSpeed)
                        li.SubItems.Add(strElapsedTime)
                        li.SubItems.Add(strPassingTime)

                        If i Mod 2 = 0 Then
                            li.BackColor = Color.AliceBlue
                        Else
                            li.BackColor = Color.White
                        End If
                        i += 1

                        Dim last As Short
                        last = dblvROPResults.Items.Count - 1
                        dblvROPResults.HideSelection = False
                        dblvROPResults.EnsureVisible(last)

                    Next
                    LastLapCount = i
                End If
            End If

        End SyncLock

        'li.SubItems.Add(CType(LapCount, String))
        'End If
    End Sub
Sorry, it's still difficult to see your data structure from this splurge of code, but I'm sure it could be simpler, for example:
    Dim L = (From laps In lapsCurrentRun Where laps.RunID = My.Settings.RunID Select laps.Lap).First()
... should select the instance of the clsLapDetail you want
Mike,

The data structure is included in the code attachement up above.

Will your suggestion allow me to iterate through all the elements in the list for a particular driver.

If not, it's no big deal as my solution isn't the most efficient, but it does work.

Sorry, a mistake in the post, it should have read Last() not First().
  Dim L = (From laps In lapsCurrentRun Where laps.RunID = My.Settings.RunID Select laps.Lap).Last()
... should select the instance of the clsLapDetail you want
Linq iterates the collection without you needing to do it.
Okay, that is the first part of the question...Now, as I iterate through, how do I add the items to the listview?  Can you dimonstrate that code?
ASKER CERTIFIED SOLUTION
Avatar of MikeToole
MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is EXACTLY what I was looking for...