How to make IEnumerable.Contains case-insensitive?

前端 未结 3 774
走了就别回头了
走了就别回头了 2020-12-09 01:34

Suppose I have a .net Array of strings.

string[] strings = new string[] { \"AbC\", \"123\", \"Xyz\", \"321\" };

If I wanted to see if the a

3条回答
  •  自闭症患者
    2020-12-09 01:48

    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(
        (lhs, rhs) => lhs.Equals(rhs, StringComparison.InvariantCultureIgnoreCase));
    
    var seq = new[]{"a","b","c","d","e"};
    
    Debug.Assert(seq.Contains("A", comparer));
    

提交回复
热议问题