?
Solved

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

Posted on 2011-05-07
9
Medium Priority
?
711 Views
Last Modified: 2013-11-11
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
         
0
Comment
Question by:indy500fan
  • 5
  • 4
9 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 35712521
Does this work?

        Dim MaxSpeed As Decimal = (From lap In DriverList(DriverID) Where lap.RunID = My.Settings.RunID _
                            Select lap.LapSpeed).Max
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 35712524
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
 

Author Comment

by:indy500fan
ID: 35712583
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 24

Expert Comment

by:Jeff Certain
ID: 35712606
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
 

Author Comment

by:indy500fan
ID: 35712785
Having some troubles...I think I see what you mean...Trying a few things..
0
 

Author Comment

by:indy500fan
ID: 35712828
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
 

Author Comment

by:indy500fan
ID: 35712925
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
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 2000 total points
ID: 35713361
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
 

Author Closing Comment

by:indy500fan
ID: 35713415
Once again, Chaosian proves they are a genius!
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question