Skip to content

Instantly share code, notes, and snippets.

@LukeWinikates
Created June 2, 2012 00:56
Show Gist options
  • Save LukeWinikates/2856000 to your computer and use it in GitHub Desktop.
Save LukeWinikates/2856000 to your computer and use it in GitHub Desktop.
Sometimes, IEnumerable<T>.Any() is the opposite of what you want...
public static class EqualityExtender
{
/// <summary>
/// Sometimes you want to say this.Name = "Bob" or "Fran". Linq lets you do:
/// new[]{"Fran", "Bob"}.Any(n=> n == this.Name);
///
/// but isn't it nicer to do:
///
/// this.Name.EqualsAnyOf(new[]{"Bob", "Fran"});
/// </summary>
public static bool EqualsAnyOf<T>(this T source, IEnumerable<T> others)
{
return others.Any(t => t.Equals(source));
}
}
[TestFixture]
public class EqualityExtenderTests {
[TestCase("a")]
[TestCase("b")]
[TestCase("z")]
public void AlphabetIncludesLetters(string letter) {
Assert.That(letter.EqualsAnyOf(new[] {
"a", "b", "z"
}));
}
[TestCase(-1)]
public void PositiveNumbersDontIncludeNegatives(int number) {
Assert.False(number.EqualsAnyOf(new[] {1, 2, 3}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment