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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeff CertainCommented:
Does this work?

        Dim MaxSpeed As Decimal = (From lap In DriverList(DriverID) Where lap.RunID = My.Settings.RunID _
                            Select lap.LapSpeed).Max
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
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.

Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
indy500fanAuthor Commented:
Having some troubles...I think I see what you mean...Trying a few things..
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.
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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
indy500fanAuthor Commented:
Once again, Chaosian proves they are a genius!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.