0

I want to make this case insensitive:

 if (usernames.Any(newName.Equals))

i have earlier used stringcomparer and regex to make things case insensitive but here the problem is that the method don't allow more arguments. I'm thankful for any help! :)

Edit: Forgot to say that usernames is a string array and newName is a string(my inputfield) if that matters.

Pavel Anikhouski
  • 19,922
  • 12
  • 45
  • 57

3 Answers3

3

You can rewrite predicate for Any a little bit

if (usernames.Any(n => newName.Equals(n, StringComparison.OrdinalIgnoreCase)))
{
    //rest of code
}
Pavel Anikhouski
  • 19,922
  • 12
  • 45
  • 57
0

Use String.Equals instead.

String.Equals(x.Username, (string)drUser["Username"], 
                   StringComparison.OrdinalIgnoreCase) 

See more here

Ygalbel
  • 4,714
  • 21
  • 30
0

You can use string.Equals which can handle NULL values:

if (usernames.Any(u => string.Equals(u, newName, 
    StringComparison.CurrentCultureIgnoreCase)))
{

}

As msdn says about Equals(String, String):

Determines whether two String objects have the same value.

Parameters a String

The first string to compare, or null.

Parameters b String

String The second string to compare, or null.

StepUp
  • 30,747
  • 12
  • 76
  • 133