score:129

Accepted answer

Use overloaded Enumerable.Contains method which accepts equality comparer:

strings.Contains("ABC", StringComparer.InvariantCultureIgnoreCase)

Also there is strings comparer in box which you can use.

score:0

If for some reason you either prefer or are forced to use StringComparison and not StringComparer, you can add an extension method as follows:

public static bool Contains(this IEnumerable<string> items, string value, StringComparison stringComparison)
{
    StringComparer stringComparer;

    switch (stringComparison)
    {
        case StringComparison.CurrentCulture:
            stringComparer = StringComparer.CurrentCulture;
            break;
        case StringComparison.CurrentCultureIgnoreCase:
            stringComparer = StringComparer.CurrentCultureIgnoreCase;
            break;
        case StringComparison.InvariantCulture:
            stringComparer = StringComparer.InvariantCulture;
            break;
        case StringComparison.InvariantCultureIgnoreCase:
            stringComparer = StringComparer.InvariantCultureIgnoreCase;
            break;
        case StringComparison.Ordinal:
            stringComparer = StringComparer.Ordinal;
            break;
        case StringComparison.OrdinalIgnoreCase:
            stringComparer = StringComparer.OrdinalIgnoreCase;
            break;
        default:
            throw new NotImplementedException();
    }

    return items.Contains(value, stringComparer);
}

More variations on how to map these can be found in this question.

score:2

I personally like this guy's LambdaComparer, which is really useful for stuff like this:

LINQ Your Collections with IEqualityComparer and Lambda Expressions

Example Usage:

var comparer = new LambdaComparer<string>(
    (lhs, rhs) => lhs.Equals(rhs, StringComparison.InvariantCultureIgnoreCase));

var seq = new[]{"a","b","c","d","e"};

Debug.Assert(seq.Contains("A", comparer));

Related Query

More Query from same tag