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(Last Speed, String))
End If
Next
However, this won't even compile...
Looking for VB.net syntax please.
Thanks in advance!
Eric
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(
'If lapsAny.Count > 0 Then
Dim LastLap As Integer
Dim LastSpeed As Decimal
For Each lap In frmROPMain.DriverLapsList(
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(Last
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
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(valu e)
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(va lue)
'Dim strLapTime As String = FormatTime.GetFullyFormatt edTime_Dec imal(CType (intLapTim e * 0.0001, String))
Dim intPassingTime As Integer = (From laps In lapsCurrentRun Where laps.RunID = My.Settings.RunID Select laps.PassingTime).ElementA t(value)
Dim strPassingTime As String = FormatTime.GetFullyFormatt edTime_Int eger(CType (intPassin gTime * 0.0001, String))
Dim intElapsedTime As Integer = (From laps In lapsCurrentRun Where laps.RunID = My.Settings.RunID Select laps.ElapsedTime).ElementA t(value)
Dim strElapsedTime As String = FormatTime.GetFullyFormatt edTime_Int eger(CType (intElapse dTime * 0.0001, String))
Dim decLapSpeed As Decimal = (From laps In lapsCurrentRun Where laps.RunID = My.Settings.RunID Select laps.LapSpeed).ElementAt(v alue)
li = dblvROPResults.Items.Add(i ntLapNumbe r)
li.SubItems.Add(decLapSpee d)
li.SubItems.Add(strElapsed Time)
li.SubItems.Add(strPassing Time)
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.HideSelecti on = False
dblvROPResults.EnsureVisib le(last)
Next
LastLapCount = i
End If
End If
End SyncLock
'li.SubItems.Add(CType(Lap Count, String))
'End If
End Sub
And it works...
SyncLock frmROPMain.DriverLapsList
Dim lapsCurrentRun = (From lap In frmROPMain.DriverLapsList(
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(valu
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(va
'Dim strLapTime As String = FormatTime.GetFullyFormatt
Dim intPassingTime As Integer = (From laps In lapsCurrentRun Where laps.RunID = My.Settings.RunID Select laps.PassingTime).ElementA
Dim strPassingTime As String = FormatTime.GetFullyFormatt
Dim intElapsedTime As Integer = (From laps In lapsCurrentRun Where laps.RunID = My.Settings.RunID Select laps.ElapsedTime).ElementA
Dim strElapsedTime As String = FormatTime.GetFullyFormatt
Dim decLapSpeed As Decimal = (From laps In lapsCurrentRun Where laps.RunID = My.Settings.RunID Select laps.LapSpeed).ElementAt(v
li = dblvROPResults.Items.Add(i
li.SubItems.Add(decLapSpee
li.SubItems.Add(strElapsed
li.SubItems.Add(strPassing
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
dblvROPResults.HideSelecti
dblvROPResults.EnsureVisib
Next
LastLapCount = i
End If
End If
End SyncLock
'li.SubItems.Add(CType(Lap
'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
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
ASKER
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.
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.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That is EXACTLY what I was looking for...
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(
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).L