Home >Backend Development >C++ >How to Find the Object with the Maximum Property Value in a Collection Using C#?
Suppose you have an Object collection containing multiple attributes. You need to find the Object with the largest specific attribute value. For example, considering a list of type objects, it has two integer attributes
and. The goal is to find and return the object with the highest attribute value. DimensionPair
Height
Although the Width
method of linq is easy to determine the maximum Height
value, obtaining the corresponding object may be challenging. To solve this problem, you can use the
.Max()
For the given scenario, the following code fragment will achieve the required results: Height
MaxBy
<code class="language-csharp">var item = items.MaxBy(x => x.Height);</code>Efficiency
: Its time complexity is O (n), unlike other methods that may be O (n^2) or O (n log n). MaxBy
The above is the detailed content of How to Find the Object with the Maximum Property Value in a Collection Using C#?. For more information, please follow other related articles on the PHP Chinese website!