-4

can you please advise me on a slicker way of saying:

if (PK.ToString() == "id" || "Id" || "ID" || "iD"))
{

}

as this throws a || operator cant be applied to instances of bool and string

finished asking the question but cant post because Q does not meet requirements please ignore this attempt to fill white space
John
  • 3,965
  • 21
  • 77
  • 163

2 Answers2

2

Use the string.Compare overload that lets you specify that you want case insensitive comparison

if(string.Compare(PK.ToString(), "ID", true) == 0)

Other options include converting to upper or lower case

if(PK.ToString().ToLower() == "id")

and

if(PK.ToString().ToUpper() == "id")

But you need to be aware of the culture because translating to upper or lower case doesn't always have the expected results depending on the culture. In which case you might find it better to use ordinal or the invariant culture.

if(PK.ToString().Equals("ID", StringComparison.OrdinalIgnoreCase))

or

if(PK.ToString().Equals("ID", StringComparison.InvariantCultureIgnoreCase))
juharr
  • 31,741
  • 4
  • 58
  • 93
1

Just convert it to lowercase:

PK.ToString().toLowerCase().Equals("id")
ergonaut
  • 6,929
  • 1
  • 17
  • 47