Solved

LINQ query to get element with highest score

Posted on 2013-11-05
8
421 Views
Last Modified: 2016-02-10
I'm trying to get an object in a list of objects that has the highest score. If there are two or more objects with equal high scores, I can get the first, either, or any.
Is the code below the best way to do it, or is there a better way?

 List<AddressCandidate> returnedCandidates = args.Results;

// Get the AddressCandidate with the highest addressCandidate score.
AddressCandidate addCand =   (
                from address in returnedCandidates
                let high = returnedCandidates.Max(highest => highest.Score)
                where address.Score == high
                select address).First();

Open in new window

0
Comment
Question by:XTO
  • 4
  • 4
8 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 39624568
I prefer using the order by clause....probably more efficient, too.
0
 

Author Comment

by:XTO
ID: 39624574
I should add this.
I'm thinking that the above can't be the most efficient way.

Here's why:

Suppose that returnedCandidates has a count of 8 objects.
As I step through the code in debug mode, the debugger goes through all 8 objects in step 4 above.
Then the debugger goes to step 5, and then returns back to step 4 for 8 more loops.
In other words, the debugger hits step 5 a total of 8 times, which means that step 4 gets looped 64 times total.
That seems slower than if I had just done a for loop and captured an object with the highest score.
0
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 250 total points
ID: 39624579
I.e. something like

(from address in returnedCandidates
                orderby address.Score
                select address).First();
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 39624583
(or, actually, LAST)
0
 

Author Comment

by:XTO
ID: 39624586
re: jensfiederer,

I'm going to try to create an example using an order by clause and post it here.
0
 

Author Comment

by:XTO
ID: 39624594
Our posts crossed.
0
 

Author Comment

by:XTO
ID: 39624654
re: jensfiederer

I tried your way and it worked correctly.

List<AddressCandidate> returnedCandidates = args.Results;
AddressCandidate addCand = (
                from address in returnedCandidates
                orderby address.Score
                select address).Last();  


Thank you.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 39624664
My pleasure
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

778 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