4

I use the followings to check the array or List is Included a value:

string[] Names= { /* */};
string target = "";

if(Array.IndexOf(Names, target) > -1)
  //Do

So is there any linq command to check it?

1
  • 2
    Why do you need a lambda expression? Commented Apr 3, 2012 at 7:40

4 Answers 4

14

Do you mean a Linq method?

If so, there is one:

Names.Contains(target)

Note there is no need for any lambda here.

Sign up to request clarification or add additional context in comments.

1 Comment

@Killercam : actually, I suspect the compiler would generate the same IL as Names.Any( s => s == target ) because there is no better way to find a match in an Array. If Names was not an array but some other kind of IEnumerable with a more efficient search algorithm, contains would be more efficient indeed.
11
   Names.Any( s => s == target );

Comments

2

Something like this?

Names.Any(n => Equals(n, target));

Comments

0
string[] Names= { /* */};
string target = "";

if(Names.Contains(target))
  //Do

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.