[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

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

0
indy500fan
Asked:
indy500fan
  • 4
  • 4
1 Solution
 
MikeTooleCommented:
>> 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()

0
 
indy500fanAuthor Commented:
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
0
 
MikeTooleCommented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
indy500fanAuthor Commented:
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.
0
 
MikeTooleCommented:

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.
0
 
indy500fanAuthor Commented:
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?
0
 
MikeTooleCommented:
After a dig through the For loop I think I understand now what you're trying to do.
Try the For each approach in the attached code


Dim lapsCurrentRun = (From lap In frmROPMain.DriverLapsList(DriverID) Where lap.RunID = My.Settings.RunID)
dim i as integer = 1
           For each laps in lapsCurrentRun.ToList()
                    Dim intLapNumber = laps.Lap
                    Dim intPassingTime = laps.PassingTime
                    Dim strPassingTime As String = FormatTime.GetFullyFormattedTime_Integer(CType(intPassingTime * 0.0001, String))
                    Dim intElapsedTime = laps.ElapsedTime
                    Dim strElapsedTime As String = FormatTime.GetFullyFormattedTime_Integer(CType(intElapsedTime * 0.0001, String))
                    Dim decLapSpeed = laps.LapSpeed
                        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

Open in new window

0
 
indy500fanAuthor Commented:
That is EXACTLY what I was looking for...
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now