Created
May 13, 2020 21:31
-
-
Save R2D221/019e6e65980fea2966571be94c6e2b2f to your computer and use it in GitHub Desktop.
LINQ Extensions to use in tandem with C# 8 Nullable Reference Types.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace System.Linq | |
{ | |
public struct RequireClass<T> where T : class { } | |
public struct RequireStruct<T> where T : struct { } | |
public static class NullableEnumerableExtensions | |
{ | |
public static IEnumerable<TSource> WhereNotNull<TSource>(this IEnumerable<TSource?> source) | |
where TSource : class | |
{ | |
return source.Where(x => !(x is null)).Select(x => x!); | |
} | |
public static IEnumerable<TSource> WhereNotNull<TSource>(this IEnumerable<TSource?> source) | |
where TSource : struct | |
{ | |
return source.Where(x => !(x is null)).Select(x => x.GetValueOrDefault()); | |
} | |
public static TSource SingleOrDefault<TSource>(this IEnumerable<TSource> source) where TSource : class => throw new NotSupportedException(); | |
public static TSource SingleOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) where TSource : class => throw new NotSupportedException(); | |
public static TSource? SingleOrNull<TSource>(this IEnumerable<TSource> source, RequireClass<TSource> _ = default) | |
where TSource : class | |
{ | |
return Enumerable.SingleOrDefault(source.Select(x => (TSource?)x)); | |
} | |
public static TSource? SingleOrNull<TSource>(this IEnumerable<TSource> source, RequireStruct<TSource> _ = default) | |
where TSource : struct | |
{ | |
return Enumerable.SingleOrDefault(source.Select(x => (TSource?)x)); | |
} | |
public static TSource? SingleOrNull<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, RequireClass<TSource> _ = default) | |
where TSource : class | |
{ | |
return Enumerable.SingleOrDefault(source.Where(predicate)); | |
} | |
public static TSource? SingleOrNull<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, RequireStruct<TSource> _ = default) | |
where TSource : struct | |
{ | |
return Enumerable.SingleOrDefault(source.Where(predicate)); | |
} | |
public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source) where TSource : class => throw new NotSupportedException(); | |
public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) where TSource : class => throw new NotSupportedException(); | |
public static TSource? FirstOrNull<TSource>(this IEnumerable<TSource> source, RequireClass<TSource> _ = default) | |
where TSource : class | |
{ | |
return Enumerable.FirstOrDefault(source.Select(x => (TSource?)x)); | |
} | |
public static TSource? FirstOrNull<TSource>(this IEnumerable<TSource> source, RequireStruct<TSource> _ = default) | |
where TSource : struct | |
{ | |
return Enumerable.FirstOrDefault(source.Select(x => (TSource?)x)); | |
} | |
public static TSource? FirstOrNull<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, RequireClass<TSource> _ = default) | |
where TSource : class | |
{ | |
return Enumerable.FirstOrDefault(source.Where(predicate)); | |
} | |
public static TSource? FirstOrNull<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, RequireStruct<TSource> _ = default) | |
where TSource : struct | |
{ | |
return Enumerable.FirstOrDefault(source.Where(predicate)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment