FruityFoundation/Base/Extensions/EnumerableExtensions.cs

28 lines
1.0 KiB
C#
Raw Normal View History

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
public static IEnumerable<TOutput> Choose<TInput, TOutput>(this IEnumerable<TInput> enumerable, Func<TInput, TOutput?> mapper)
where TOutput : class? =>
2022-12-23 12:34:30 -05:00
enumerable
.Select(mapper)
.Where(x => x is not null)
.Cast<TOutput>();
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
}