Created
September 27, 2017 14:37
-
-
Save einarwh/ce264d577d13a7a76a1f2c861d19c411 to your computer and use it in GitHub Desktop.
This file contains 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
public static class NullableLinqExtensions | |
{ | |
public static TTarget? Select<TSource, TTarget>(this TSource? t, Func<TSource, TTarget> selector) | |
where TSource : struct | |
where TTarget : struct | |
{ | |
return t.HasValue ? (TTarget?) selector(t.Value) : null; | |
} | |
public static TSource? Where<TSource>(this TSource? t, Func<TSource, bool> predicate) | |
where TSource : struct | |
{ | |
return t.HasValue && predicate(t.Value) ? t : null; | |
} | |
public static bool Contains<TSource>(this TSource? t, TSource v) | |
where TSource : struct | |
{ | |
return t.Contains(v, EqualityComparer<TSource>.Default); | |
} | |
public static bool Contains<TSource>(this TSource? t, TSource v, IEqualityComparer<TSource> comparer) | |
where TSource : struct | |
{ | |
return t.HasValue && comparer.Equals(t.Value, v); | |
} | |
public static bool Any<TSource>(this TSource? t) | |
where TSource : struct | |
{ | |
return t.HasValue; | |
} | |
public static bool Any<TSource>(this TSource? t, Func<TSource, bool> predicate) | |
where TSource : struct | |
{ | |
return t.HasValue && predicate(t.Value); | |
} | |
public static int Count<TSource>(this TSource? t) | |
where TSource : struct | |
{ | |
return t.HasValue ? 1 : 0; | |
} | |
public static int Count<TSource>(this TSource? t, Func<TSource, bool> predicate) | |
where TSource : struct | |
{ | |
return t.HasValue && predicate(t.Value) ? 1 : 0; | |
} | |
public static TSource First<TSource>(this TSource? t) | |
where TSource : struct | |
{ | |
return t.Value; | |
} | |
public static TAccumulate Aggregate<TSource, TAccumulate>( | |
this TSource? t, | |
TAccumulate seed, | |
Func<TAccumulate, TSource, TAccumulate> combine) | |
where TSource : struct | |
{ | |
return t.HasValue ? combine(seed, t.Value) : seed; | |
} | |
public static TSource FirstOrDefault<TSource>(this TSource? t) | |
where TSource : struct | |
{ | |
return t.HasValue ? t.Value : default(TSource); | |
} | |
public static TTarget? SelectMany<TSource, TTarget>(this TSource? t, Func<TSource, TTarget?> selector) | |
where TSource : struct | |
where TTarget : struct | |
{ | |
return t.HasValue ? selector(t.Value) : null; | |
} | |
public static TResult? SelectMany<TSource, TIntermediate, TResult>(this TSource? t, Func<TSource, TIntermediate?> selector, Func<TSource, TIntermediate, TResult> resultSelector) | |
where TSource : struct | |
where TIntermediate : struct | |
where TResult : struct | |
{ | |
return t.SelectMany(selector).Select(it => resultSelector(t.Value, it)); | |
} | |
} | |
void Lol(DateTime? maybeDateTime) | |
{ | |
var maybeTomorrow = | |
from dt in maybeDateTime | |
where dt.DayOfWeek == DayOfWeek.Friday | |
select dt.AddDays(1); | |
maybeTomorrow.Any().Dump(); | |
maybeTomorrow.Count().Dump(); | |
} | |
TimeSpan? DifferencePerchance(DateTime? maybeThis, DateTime? maybeThat) | |
{ | |
return | |
from dt1 in maybeThis | |
from dt2 in maybeThat | |
select (dt2 - dt1); | |
} | |
void Main() | |
{ | |
DifferencePerchance(DateTime.Today, DateTime.Today.AddHours(1).AddMinutes(2)).Dump(); | |
DifferencePerchance(DateTime.Today, null).Dump(); | |
DateTime? whoknows1 = DateTime.Today; | |
DateTime? whoknows2 = null; | |
TimeSpan? maybetime1 = TimeSpan.FromDays(2); | |
TimeSpan? maybetime2 = null; | |
whoknows1.Contains(DateTime.Today).Dump(); | |
whoknows2.Contains(DateTime.Today).Dump(); | |
whoknows2.FirstOrDefault().Dump(); | |
// whoknows2.First().Dump(); | |
maybetime1.Aggregate(DateTime.Today, (dt, ts) => dt.Add(ts)).Dump(); | |
maybetime2.Aggregate(DateTime.Today, (dt, ts) => dt.Add(ts)).Dump(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment