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
  • 21,776
  • 12
  • 51
  • 66

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
  • 21,776
  • 12
  • 51
  • 66
0

Use String.Equals instead.

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

See more here

Ygalbel
  • 5,214
  • 1
  • 24
  • 32
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
  • 36,391
  • 15
  • 88
  • 148