Skip to content

Instantly share code, notes, and snippets.

@einarwh
Last active August 25, 2025 11:14
Show Gist options
  • Save einarwh/c0ca5b262b1ee85aca5b7f3a42a7c041 to your computer and use it in GitHub Desktop.
Save einarwh/c0ca5b262b1ee85aca5b7f3a42a7c041 to your computer and use it in GitHub Desktop.
SelectNotNull
public static class Ext
{
public static IEnumerable<TR> SelectNotNull<T, TR>(this IEnumerable<T> source, Func<T, TR?> fn) where TR : struct
{
return source.Select(fn).Where(it => it != null).Cast<TR>();
}
public static IEnumerable<TR> SelectNotNull<T, TR>(this IEnumerable<T> source, Func<T, TR?> fn) where TR : class
{
return source.Select(fn).Where(it => it != null).Cast<TR>();
}
public static IEnumerable<TR> SelectNotNullNope<T, TR>(this IEnumerable<T> source, Func<T, TR?> fn)
{
return source.Select(fn).Where(it => it != null).Cast<TR>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment