Created
July 14, 2016 13:31
-
-
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)
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
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