0

I have something like this:

string configKeys = "othewr|RDX|MDX";

and wrote a code like this to see if value "OTHER" exists in that list

List<string> values = configKeys.Split('|').ToList();
var b = values.Find(item => item.Trim().ToUpper() == "OTHER").FirstOrDefault();

but for example because I have typed it wrong "othewr" so it crashes, but just want it to tell me if it exists or not as a boolean. How can I change the code to do that and not crash?

Bohn
  • 26,091
  • 61
  • 167
  • 254
  • 1
    If you want help fixing your code that "crashes", you need to a) show the full code so others can look at it and b) explain what you expect it to do and what it actually does (i.e. **how** it "crashes"). If your code actually is irrelevant and you just want the answer to _"How can I case-insensitively search a string's presence in a pipe-character separated list of strings"_, then I'd suggest searching. Also, I did not ask what you tried to solve your original problem (which is writing this code that does not work); I asked what you tried to get this code to work. – CodeCaster Nov 30 '15 at 16:12
  • Perhaps try Exists instead of Find? – sschimmel Nov 30 '15 at 16:18
  • @CodeCaster, did someone make you angry? You marked the question as a duplicate of the question that is related to this one by no means. – Giorgi Nakeuri Nov 30 '15 at 16:47
  • @Giorgi if you'd follow the comments a bit, which I'm sure you did, you'll realise that OP does not want help getting their code fixed, they want their question title answered - which the duplicate does. – CodeCaster Nov 30 '15 at 16:49

1 Answers1

1

Use Any. If the predicate evaluates for at least one value in the collection it returns true, else false:

var b = values.Any(item => item.Trim().ToUpper() == "OTHER");
Giorgi Nakeuri
  • 35,155
  • 8
  • 47
  • 75