"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Find the DimensionPair with the Maximum Height Using LINQ?

How to Find the DimensionPair with the Maximum Height Using LINQ?

Posted on 2025-03-23
Browse:354

How to Find the DimensionPair with the Maximum Height Using LINQ?

Use LINQ query to determine the maximum height value

question:

Given a collection of objects (DimensionPair) containing two integer properties (height and width), your goal is to use a LINQ query to identify and retrieve objects with the maximum height value.

Solution:

To achieve your goals, consider using the MaxBy extension method provided by the MoreLINQ library. This method allows you to determine the maximum value of a specific property in the collection.

In your case, you can implement the solution as follows:

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

The result stored in the item variable represents the object with the highest height value in the items collection.

Advantages of using MaxBy:

Compared with other methods, using MaxBy has the following advantages:

  • Time Complexity: It runs with O(n) time complexity, unlike other methods that may require O(n^2) or O(n log n) complexity.
  • Single-pass operation: Unlike the solution of two iteration sequences, MaxBy performs all operations in a single pass.
  • Simplicity: The code is simple and easy to understand and clearly represents the solution.
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3