DD.WellWorkover.Cloud/AsbCloudInfrastructure/LinqExtensions.cs
2024-08-19 10:01:07 +05:00

39 lines
1.1 KiB
C#

// Ignore Spelling: Linq
using System;
using System.Collections.Generic;
using System.Linq;
namespace AsbCloudInfrastructure;
public static class LinqExtensions
{
public static TProp? MaxOrDefault<TObj, TProp>(this IEnumerable<TObj> enumerable, Func<TObj, TProp> getter)
where TProp : struct
{
var value = MaxByOrDefault(enumerable, getter);
if (value is null)
return null;
return getter(value);
}
public static TObj? MaxByOrDefault<TObj, TProp>(this IEnumerable<TObj> enumerable, Func<TObj, TProp> getter)
{
return enumerable.OrderByDescending(getter).FirstOrDefault();
}
public static TProp? MinOrDefault<TObj, TProp>(this IEnumerable<TObj> enumerable, Func<TObj, TProp> getter)
where TProp : struct
{
var value = MinByOrDefault(enumerable, getter);
if (value is null)
return null;
return getter(value);
}
public static TObj? MinByOrDefault<TObj, TProp>(this IEnumerable<TObj> enumerable, Func<TObj, TProp> getter)
{
return enumerable.OrderBy(getter).FirstOrDefault();
}
}