Link to home
Start Free TrialLog in
Avatar of indy500fan
indy500fan

asked on

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
         
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Does this work?

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

ASKER

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.

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
Having some troubles...I think I see what you mean...Trying a few things..
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.
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
ASKER CERTIFIED SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America 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
Once again, Chaosian proves they are a genius!