I will go with Max , as it is specially designed for this purpose. The sort to find the Max value seems too big.
Also, I would not use Where to find max, but Single - since we only need the Single value here.
var maxValOfProperty = collection.Max(x => x.Property); var itemWithMaxPropValue = collection .Single(x => x.Property == maxValueOfProperty);
Or, alternatively, using First (if the collection contains duplicates of the maximum value)
var maxValOfProperty = collection.Max(x => x.Property); var itemWithMaxPropValue = collection .First(x => x.Property == maxValueOfProperty);
Or, using MoreLINQ (as suggested by Kathi ), you can do this with MaxBy :
var itemWithMaxPropValue = collection.MaxBy(x => x.Property);
Mark the message , reply to Jon Skeet .
Ian
source share