?
Solved

LINQ query to get element with highest score

Posted on 2013-11-05
8
Medium Priority
?
435 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 1000 total points
ID: 39624579
I.e. something like

(from address in returnedCandidates
                orderby address.Score
                select address).First();
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

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