public static class LinqExtension { public static T MaxBy(this IEnumerable en, Func evaluate) where TR : IComparable { return en.Select(t => new Tuple (t, evaluate(t))) .Aggregate((max, next) => next.Item2.CompareTo(max.Item2) > 0 ? next : max).Item1; } public static T MinBy (this IEnumerable en, Func evaluate) where TR : IComparable { return en.Select(t => new Tuple (t, evaluate(t))) .Aggregate((max, next) => next.Item2.CompareTo(max.Item2) < 0 ? next : max).Item1; } }