Class XEnumerable
- Namespace
- Prefrontal.Common.Extensions
- Assembly
- Prefrontal.Core.dll
Provides extension methods for IEnumerable<T>.
public static class XEnumerable
- Inheritance
-
XEnumerable
- Inherited Members
Methods
Except<T>(IEnumerable<T>, params T[])
Produces the set difference of two sequences by using the default equality comparer to compare values.
public static IEnumerable<T> Except<T>(this IEnumerable<T> enumerable, params T[] exceptions)
Parameters
enumerable
IEnumerable<T>exceptions
T[]
Returns
- IEnumerable<T>
A sequence that contains the set difference of the elements of two sequences.
Type Parameters
T
Exceptions
- ArgumentNullException
first
orsecond
is null.
JoinVerbose<T>(IEnumerable<T>, string, string)
Just like Join<T>(IEnumerable<T>, string), but with a last separator.
public static string JoinVerbose<T>(this IEnumerable<T> values, string separator = ", ", string lastSeparator = " and ")
Parameters
values
IEnumerable<T>A collection that contains the strings to concatenate.
separator
stringThe string to use as a separator.
separator
is included in the returned string only ifvalues
has more than one element.lastSeparator
stringThe separator to use before the last item.
Returns
- string
A string that consists of the elements of
values
delimited by theseparator
string and finally thelastSeparator
string -or- string.Empty ifvalues
has zero elements.
Type Parameters
T
Join<T>(IEnumerable<T>, string)
Just like Join(string, IEnumerable<string>), but with a default separator ", ".
public static string Join<T>(this IEnumerable<T> values, string separator = ", ")
Parameters
values
IEnumerable<T>A collection that contains the strings to concatenate.
separator
stringThe string to use as a separator.
separator
is included in the returned string only ifvalues
has more than one element.
Returns
- string
A string that consists of the elements of
values
delimited by theseparator
string.-or-
Empty if
values
has zero elements.
Type Parameters
T
Exceptions
- ArgumentNullException
values
is null.- OutOfMemoryException
The length of the resulting string overflows the maximum allowed length (Int32.MaxValue).
Tap<TEnumerable>(TEnumerable, Action<TEnumerable>)
Executes an action on the TEnumerable
and returns the original TEnumerable
.
Handy when you need to run a block of code in an expression for side effects.
Example:
var sum = new List<double> { 1, 2, 3 }
.Select(x => x * 2)
.Tap(list => Console.WriteLine("Doubled: {list.JoinVerbose()}"))
.Where(x => x > 3)
.Tap(list => Console.WriteLine("Filtered: {list.JoinVerbose()}"))
.Sum();
public static TEnumerable Tap<TEnumerable>(this TEnumerable enumerable, Action<TEnumerable> action) where TEnumerable : IEnumerable
Parameters
enumerable
TEnumerableaction
Action<TEnumerable>
Returns
- TEnumerable
Type Parameters
TEnumerable
Tap<TEnumerable, T>(TEnumerable, Func<TEnumerable, T>, out T)
Executes a function on the TEnumerable
,
assigns the result to output
,
and returns the original TEnumerable
.
Handy when you need to run a block of code in an expression for side effects.
Example:
var allExceptMax = new List<double> { 1, 2, 3 }
.Select(x => x * 2)
.Tap(Enumerable.Max, out var max)
.Except(max)
.ToList();
public static TEnumerable Tap<TEnumerable, T>(this TEnumerable enumerable, Func<TEnumerable, T> func, out T output) where TEnumerable : IEnumerable<T>
Parameters
enumerable
TEnumerablefunc
Func<TEnumerable, T>output
T
Returns
- TEnumerable
Type Parameters
TEnumerable
T
Thru<TEnumerable, TOut>(TEnumerable, Func<TEnumerable, TOut>)
Maps the TEnumerable
to a single TOut
value.
Handy when you need a run a block of code in an expression.
Example:
var avgAndStd = new List<double> { 1, 2, 3 }
.Select(x => x * 2)
.Thru(list =>
{
double average = list.Average();
double standardDeviation = Math.Sqrt(list.Average(x => Math.Pow(x - average, 2)));
return (average, standardDeviation);
});
public static TOut Thru<TEnumerable, TOut>(this TEnumerable enumerable, Func<TEnumerable, TOut> func) where TEnumerable : IEnumerable
Parameters
enumerable
TEnumerablefunc
Func<TEnumerable, TOut>
Returns
- TOut
Type Parameters
TEnumerable
TOut
ToListIfNotEmpty<T>(IEnumerable<T>)
Just like ToList<TSource>(IEnumerable<TSource>),
but returns null if the enumerable
is null or empty.
public static List<T>? ToListIfNotEmpty<T>(this IEnumerable<T> enumerable)
Parameters
enumerable
IEnumerable<T>The enumerable to convert.
Returns
Type Parameters
T
The type of the elements of
enumerable
.
WhereNotNull<T>(IEnumerable<T?>)
Returns non-null elements from the enumerable
.
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> enumerable) where T : struct
Parameters
enumerable
IEnumerable<T?>
Returns
- IEnumerable<T>
Type Parameters
T
WhereNotNull<T>(IEnumerable<T?>)
Returns non-null elements from the enumerable
.
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> enumerable) where T : class
Parameters
enumerable
IEnumerable<T>
Returns
- IEnumerable<T>
Type Parameters
T