0

I'm trying to use Linq to determine if a string is NOT in an array. The code I'm using is:

if (!stringArray.Any(soughtString.Contains)){
            doStuff();}

but it's not working. Obviously creating a foreach loop would suffice, but I'd like to understand why this line isn't working. And yes, the file has using System.Linq;

1
  • 2
    array.Contains("foo") ? Commented Mar 31, 2014 at 19:51

2 Answers 2

5

You're not asking if the string is not in the array, you're asking if none of the strings in the array are sub-strings in some other string. Apparently at least one is, even though it's not equal.

You just want to do a simple Contains check:

if(!stringArray.Contains(soughtString))
Sign up to request clarification or add additional context in comments.

Comments

3

You are currently passing the "Any" function the "Contains" method (which is then being passed each string in the array). In other, words:

array.Any(s => soughtString.Contains(s));

Likely, you want it the other way:

array.Any(s => s.Contains(soughtString));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.