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

Find Max of Values using LINQ and Subquery

I have a List of doubles, sorted ascending. How can I use LINQ to query the list for the maximum double in the list that is less than a random value I choose?
0
diablo089
Asked:
diablo089
  • 3
  • 3
  • 3
2 Solutions
 
Craig WagnerSoftware ArchitectCommented:
double maxDouble = doubleList.Where( d => d > someRandomValue ).OrderByDescending().FirstOrDefault() );
0
 
Bob LearnedCommented:
You forgot the Max operator...
0
 
diablo089Author Commented:
Can you guys explain a little as to how this works? Thanks.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Bob LearnedCommented:
What kind of explanation do you need?  Do you need to know how to compose a LINQ statement?  Do you need to know what the operator extension methods do?
0
 
Craig WagnerSoftware ArchitectCommented:
The statement I posted doesn't need a Max operator, it sorts the items descending and then grabs the first one (i.e. the max). But now that you mention it, this would also work.

double maxDouble = doubleList.Where( d => d > someRandomValue ).Max();
0
 
diablo089Author Commented:
All of the above.
0
 
Craig WagnerSoftware ArchitectCommented:
As to how it works.

doubleList is your list of doubles
Where( d => d > someRandomValue ) gives you a subset of that list where any element from the list (d) is greater than someRandomValue
Max() takes the maximum item from the list returned by the Where() method.
0
 
diablo089Author Commented:
Thanks Craig. I appreciate your effort.
0
 
Bob LearnedCommented:
The order of operations, says that you sort after the Where clause...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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