• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

LINQ query to get element with highest score

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
XTO
Asked:
XTO
  • 4
  • 4
1 Solution
 
Jens FiedererTest Developer/ValidatorCommented:
I prefer using the order by clause....probably more efficient, too.
0
 
XTOAuthor Commented:
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
 
Jens FiedererTest Developer/ValidatorCommented:
I.e. something like

(from address in returnedCandidates
                orderby address.Score
                select address).First();
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Jens FiedererTest Developer/ValidatorCommented:
(or, actually, LAST)
0
 
XTOAuthor Commented:
re: jensfiederer,

I'm going to try to create an example using an order by clause and post it here.
0
 
XTOAuthor Commented:
Our posts crossed.
0
 
XTOAuthor Commented:
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
 
Jens FiedererTest Developer/ValidatorCommented:
My pleasure
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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now