Last active
October 9, 2021 02:56
-
-
Save vabka/c675c5b7e3b4020ed1bebaaa6cf3235e 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
// And same for IQueryable | |
public static class EnumerableExtensions | |
{ | |
public static T? SingleOrNull<T>(this IEnumerable<T> seq, Func<T, bool> predicate) where T : struct | |
{ | |
var data = seq.Where(predicate).Take(2).ToArray(); | |
if (data.Length == 2) | |
throw new InvalidOperationException(); | |
if (data.Length == 0) | |
return null; | |
return data[0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment