Linq provides a convenient way to find objects with the maximum attribute value in the set. Let's take a look at an example:
Assuming we have a list of
objects, each object has and DimensionPair
attributes: Height
Width
<code class="language-csharp">public class DimensionPair { public int Height { get; set; } public int Width { get; set; } }</code>
We can use the method to easily achieve: Height
Through Lambda expression MaxBy()
, we told linq to compare the
<code class="language-csharp">var maxItem = items.MaxBy(x => x.Height);</code>
This method has the following advantages: x => x.Height
Height
maxItem
Efficiency:
only need to traverse the collection once, and the time complexity is O (n).
MaxBy()
The above is the detailed content of How to Find the Object with the Maximum Property Value Using LINQ?. For more information, please follow other related articles on the PHP Chinese website!