?
Solved

LINQ query to get element with highest score

Posted on 2013-11-05
8
Medium Priority
?
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

800 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