How do I get a max value from an list using LINQ?

Friends,

I have the following LINQ qry (Thanks to Chaosian):

 For Each Driver In ROPDrivers_AL

            Dim DriverID As String = Driver
            'Phase 1
         Dim lapCount As Integer = (From lap In DriverList(DriverID) Where lap.LapSpeed >= 200 AndAlso lap.LapSpeed < 205).Count

...
Next

I'd now like to get the Max(lap.LapSpeed) (where lap.RuniD=varRunID and the last LapSpeed (where lap.RuniD=varRunID (-- basicially that value would be lapspeed where  Max(lap.Lapcount).

Any suggestion on what those qry's would be?

I would think it would be something like:

            Dim MaxSpeed As Decimal = (From lap In DriverList(DriverID) Where lap.RunID = My.Settings.RunID _
                                        Select lap.LapSpeed _
                                        Order By LapSpeed Descending).Single

            Dim LastSpeed As Decimal = (From lap In DriverList(DriverID) Where lap.RunID = My.Settings.RunID _
                                        Select lap.LapSpeed _
                                        Order By lapCount Descending).Single


But they throw exceptions when it runs:

{"Sequence contains no elements"}

Looking for VB.net syntax help, please.

Thanks in advance!

Eric
         
indy500fanAsked:
Who is Participating?
 
Jeff CertainCommented:
The only minor tweak I'd suggest would be to store the results of your LINQ query locally, then reuse it. For example:

        Dim MaxSpeed As Decimal = 0.0
        Dim LastSpeed As Decimal = 0.0
        Dim LastLap As Integer = 0

        Dim laps = (From lap In DriverList(DriverID) Where lap.RunID = My.Settings.RunId)
        If laps.Count > 0 Then
            MaxSpeed = (From lap In laps Select lap.LapSpeed).Max
            LastLap = (From lap In laps Select lap.Lap).Max
            LastSpeed = (From lap In laps Where lap.Lap = LastLap Select lap.LapSpeed).Single
        End If

0
 
Jeff CertainCommented:
Does this work?

        Dim MaxSpeed As Decimal = (From lap In DriverList(DriverID) Where lap.RunID = My.Settings.RunID _
                            Select lap.LapSpeed).Max
0
 
Jeff CertainCommented:
The last lap speed can be retrieved a little more... creatively. :)

        Dim lapList As List(Of clsLapDetail) = DriverList(driverId)
        Dim lastSpeed As Decimal = lapList.Last.LapSpeed
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
indy500fanAuthor Commented:
Unfortunately, I don't think I can rely on that Chaosian....I have to make sure I am looking at a particular run

There are multiple runs and I don't know if the last record is going to really be the latest run.  

I need that run qualifier.

As far as the max speed, I need to put some alternate value if no elements exist for that runid.

I am getting an exception because I am trying to pull a value that doesn't exist in the runID i am passing in.

0
 
Jeff CertainCommented:
In that case, try a two-stage approach.

        Dim lastSpeed As Decimal
        Dim lastRun = From lap In DriverList(driverId) Where lap.RunId = My.Settings.RunId
        If lastrun.count = 0 Then
            ' handle the error case
        Else
            lastSpeed = lastRun.First.LapSpeed
        End If
0
 
indy500fanAuthor Commented:
Having some troubles...I think I see what you mean...Trying a few things..
0
 
indy500fanAuthor Commented:
Okay, i think this is what you are suggesting...,but

It never finds a case where it is true (lastRun <> 0), or it is doing something to 'lock up' the development environment.  It's so bad that I have to close the project and re-open it because it says that it is still being used by other processes.

Dim MaxSpeed As Decimal = 0.0
Dim LastSpeed As Decimal = 0.0
Dim lastRun As Integer = (From lap In DriverList(DriverID) Where lap.RunID = My.Settings.RunID).Count
            If lastRun = 0 Then
            '    ' handle the error case
            Else
               MaxSpeed = (From lap In DriverList(DriverID) Where lap.RunID = My.Settings.RunID _
                       Select lap.LapSpeed).Max

            LastSpeed = (From lap In DriverList(DriverID) Where lap.RunID = My.Settings.RunID _
                                       Select lap.LapSpeed _
                                   Order By lapCount Descending).Single
            End If


Any other thoughts, cause this one sure won't work.
0
 
indy500fanAuthor Commented:
Ea gahds!!!  I was having a synclock issue during testing...

Here is what I have so far:

                Dim MaxSpeed As Decimal = 0.0
                Dim LastSpeed As Decimal = 0.0
                Dim LastLap As Integer = 0

                Dim lastRun As Integer = (From lap In DriverList(DriverID) Where lap.RunID = My.Settings.RunID).Count
                If lastRun = 0 Then
                    '    ' handle the error case
                Else
                    MaxSpeed = (From lap In DriverList(DriverID) Where lap.RunID = My.Settings.RunID _
                             Select lap.LapSpeed).Max

                    LastLap = (From lap In DriverList(DriverID) Where lap.RunID = My.Settings.RunID _
                             Select lap.Lap).Max

                    LastSpeed = (From lap In DriverList(DriverID) Where lap.RunID = My.Settings.RunID AndAlso lap.Lap = LastLap _
                             Select lap.LapSpeed).Single

                End If


Any better ways of getting the data I need, seeing what I've got now?

Thanks,
Eric
0
 
indy500fanAuthor Commented:
Once again, Chaosian proves they are a genius!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.