DD.WellWorkover.Cloud/AsbCloudInfrastructure/LinqExtensions.cs

39 lines
1.1 KiB
C#
Raw Normal View History

// Ignore Spelling: Linq
using System;
using System.Collections.Generic;
using System.Linq;
2024-08-19 10:01:07 +05:00
namespace AsbCloudInfrastructure;
public static class LinqExtensions
{
2024-08-19 10:01:07 +05:00
public static TProp? MaxOrDefault<TObj, TProp>(this IEnumerable<TObj> enumerable, Func<TObj, TProp> getter)
where TProp : struct
{
2024-08-19 10:01:07 +05:00
var value = MaxByOrDefault(enumerable, getter);
if (value is null)
return null;
return getter(value);
}
2024-08-19 10:01:07 +05:00
public static TObj? MaxByOrDefault<TObj, TProp>(this IEnumerable<TObj> enumerable, Func<TObj, TProp> getter)
{
return enumerable.OrderByDescending(getter).FirstOrDefault();
}
2024-08-19 10:01:07 +05:00
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);
}
2024-08-19 10:01:07 +05:00
public static TObj? MinByOrDefault<TObj, TProp>(this IEnumerable<TObj> enumerable, Func<TObj, TProp> getter)
{
return enumerable.OrderBy(getter).FirstOrDefault();
}
}