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?
    – BoltClock
    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.

1
  • 3
    @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.
    – Falanwe
    Apr 3, 2012 at 8:39
11
   Names.Any( s => s == target );
2

Something like this?

Names.Any(n => Equals(n, target));
0
string[] Names= { /* */};
string target = "";

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.