I am trying to get the maximum values out of a list but if there are multiple max values then I want to get all the max values.
For instance I have: Name1, 31 Name2, 35 Name3, 33 Name4, 35
And I want to get: {Name2, 35} AND {Name4, 35}
I tried using MaxBy()
;
But that only returns the first item(Name2, 35). Any help will be greatly appreciated
struct Amounts
{
public string Name;
public int Total;
}
Amount highestAmount = amounts.MaxBy(x => x.Total);
You can do it by using GroupBy first then using MaxBy on each key. Here is an extension method:
Here is a working demo:
output