Created
February 14, 2011 21:51
-
-
Save jamesantrobus/826630 to your computer and use it in GitHub Desktop.
Newtonsoft.Json StringUtils
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
| Newtonsoft.Json StringUtils line 353 | |
| public static TSource ForgivingCaseSensitiveFind<TSource>(this IEnumerable<TSource> source, Func<TSource, string> valueSelector, string testValue) | |
| { | |
| if (source == null) | |
| throw new ArgumentNullException("source"); | |
| if (valueSelector == null) | |
| throw new ArgumentNullException("valueSelector"); | |
| var caseInsensitiveResults = source.Where(s => string.Compare(valueSelector(s), testValue, StringComparison.OrdinalIgnoreCase) == 0); | |
| if (caseInsensitiveResults.Count() <= 1) | |
| { | |
| return caseInsensitiveResults.SingleOrDefault(); | |
| } | |
| else | |
| { | |
| // multiple results returned. now filter using case sensitivity | |
| var caseSensitiveResults = source.Where(s => string.Compare(valueSelector(s), testValue, StringComparison.Ordinal) == 0); | |
| return caseSensitiveResults.SingleOrDefault(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment