Web Developer, Software Engineer and Mixed Language Artist
RSS icon Email icon Home icon
  • ContainsAny & EquelsAny

    Posted on November 2nd, 2010 Jamie No comments

    This post is a bit of a reference for myself and also a handy tip for those wondering how to perform Contains and Equels on a collection of strings.

    For the purposes I needed I simply wanted to check if a string Equels Any or Contains Any of an array of strings. I put these together as a couple of extension methods:

    ?View Code CSHARP
    public static bool ContainsAny(this string input, string[] items)
            {
                return items.Any(input.Contains);
            }
    ?View Code CSHARP
            public static bool EqualsAny(this string input, string[] items)
            {
                return items.Any(input.Equels);
            }

    That’s all that’s required.

    This means we can now do things like:

    ?View Code CSHARP
    bool isWomenOrUnisex = category.EquelsAny(new []{"women","unisex"});

    Simples!

    Leave a reply