Skip to content

Instantly share code, notes, and snippets.

@lisardggY
Created July 14, 2016 13:31
Show Gist options
  • Save lisardggY/f3740bab3429dd4c3ad92c4b2fc16738 to your computer and use it in GitHub Desktop.
Save lisardggY/f3740bab3429dd4c3ad92c4b2fc16738 to your computer and use it in GitHub Desktop.
SelectIfNotNull - Like Select, but returns only those mapped results that aren't null. Equivalent to items.Select().Where(x => x != null)
public static IEnumerable<U> SelectIfNotNull<T, U>(this IEnumerable<T> source, Func<T, U> mapper)
{
foreach (var sourceItem in source)
{
var resultItem = mapper(sourceItem);
if (resultItem != null)
{
yield return resultItem;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment