xxxxxxxxxx
public static T FirstWithMax<T, TValue>(this IEnumerable<T> items, Func<T, TValue> propSelector)
where TValue : IComparable<TValue>
{
return items.Aggregate((current, next) => propSelector.Invoke(current).CompareTo(propSelector.Invoke(next)) < 0 ? next : current);
}
xxxxxxxxxx
private void Test()
{
test v1 = new test();
v1.Id = 12;
test v2 = new test();
v2.Id = 12;
test v3 = new test();
v3.Id = 12;
List<test> arr = new List<test>();
arr.Add(v1);
arr.Add(v2);
arr.Add(v3);
test max = arr.OrderByDescending(t => t.Id).First();
}
class test
{
public int Id { get; set; }
}
xxxxxxxxxx
var oldest = People.OrderBy(p => p.DateOfBirth ?? DateTime.MaxValue).First();