forked from ddrilling/AsbCloudServer
40 lines
1.2 KiB
C#
40 lines
1.2 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();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|