Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 272299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:17:41+00:00 2026-05-12T00:17:41+00:00

I have a list of objects that have two int properties. The list is

  • 0

I have a list of objects that have two int properties. The list is the output of another linq query. The object:

public class DimensionPair  
{
    public int Height { get; set; }
    public int Width { get; set; }
}

I want to find and return the object in the list which has the largest Height property value.

I can manage to get the highest value of the Height value but not the object itself.

Can I do this with Linq? How?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:17:42+00:00Added an answer on May 12, 2026 at 12:17 am

    We have an extension method to do exactly this in MoreLINQ. You can look at the implementation there, but basically it’s a case of iterating through the data, remembering the maximum element we’ve seen so far and the maximum value it produced under the projection.

    In your case you’d do something like:

    var item = items.MaxBy(x => x.Height);
    

    This is better (IMO) than any of the solutions presented here other than Mehrdad’s second solution (which is basically the same as MaxBy):

    • It’s O(n) unlike the previous accepted answer which finds the maximum value on every iteration (making it O(n^2))
    • The ordering solution is O(n log n)
    • Taking the Max value and then finding the first element with that value is O(n), but iterates over the sequence twice. Where possible, you should use LINQ in a single-pass fashion.
    • It’s a lot simpler to read and understand than the aggregate version, and only evaluates the projection once per element
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have a class called MyClass that has two properties (int Id and
i have the following two objects public partial class ProgramObj { public int id;
I have two threads, a producer thread that places objects into a generic List
I have a parent object called Page that has a List of objects called
I want to have a stl list of objects where each object contains two
Let's say I have a list of User objects with two properties...ID and Name
I have an object, that has many properties but the only two to worry
I have a manager class maintaining a list of objects. Each Object has a
I have a class that contains a list of objects. I previously had this
Suppose we have these business objects with some properties in common: public class A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.