Skip to content

Instantly share code, notes, and snippets.

@jamesantrobus
Created February 14, 2011 21:51
Show Gist options
  • Save jamesantrobus/826630 to your computer and use it in GitHub Desktop.
Save jamesantrobus/826630 to your computer and use it in GitHub Desktop.
Newtonsoft.Json StringUtils
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