2022-12-23 12:38:51 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace FruityFoundation.Base.Extensions;
|
2022-09-23 21:07:42 -04:00
|
|
|
|
|
|
|
|
|
public static class EnumerableExtensions
|
|
|
|
|
{
|
|
|
|
|
public static IEnumerable<T> ConditionalConcat<T>(this IEnumerable<T> enumerable, bool isConditionValid, IEnumerable<T> second) =>
|
|
|
|
|
!isConditionValid ? enumerable : enumerable.Concat(second);
|
2022-12-23 12:34:30 -05:00
|
|
|
|
|
2022-09-23 21:08:00 -04:00
|
|
|
|
public static IEnumerable<T> ConditionalWhere<T>(this IEnumerable<T> enumerable, bool isConditionValid, Func<T, bool> pred) =>
|
|
|
|
|
!isConditionValid ? enumerable : enumerable.Where(pred);
|
2022-12-23 12:34:30 -05:00
|
|
|
|
|
2022-12-23 13:39:33 -05:00
|
|
|
|
public static IEnumerable<TOutput> Choose<TInput, TOutput>(this IEnumerable<TInput> enumerable, Func<TInput, TOutput?> mapper)
|
2022-12-23 13:50:06 -05:00
|
|
|
|
where TOutput : class? =>
|
2022-12-23 12:34:30 -05:00
|
|
|
|
enumerable
|
|
|
|
|
.Select(mapper)
|
|
|
|
|
.Where(x => x is not null)
|
|
|
|
|
.Cast<TOutput>();
|
2022-12-23 13:39:33 -05:00
|
|
|
|
|
|
|
|
|
public static IEnumerable<TOutput> Choose<TInput, TOutput>(this IEnumerable<TInput> enumerable, Func<TInput, TOutput?> mapper)
|
|
|
|
|
where TOutput : struct =>
|
|
|
|
|
enumerable
|
|
|
|
|
.Select(mapper)
|
|
|
|
|
.Where(x => x.HasValue)
|
|
|
|
|
.Select(x => x!.Value);
|
2022-09-23 21:07:42 -04:00
|
|
|
|
}
|