Use linq to check string value in string array or List in c #

I am using the following steps to check an array or list. Included value:

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

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

      

So, is there a linq command to test this?

+3


source to share


4 answers


Do you mean the Linq method?

If yes, then one:



Names.Contains(target)

      

Note that there is no need for a lambda here.

+13


source


   Names.Any( s => s == target );

      



+11


source


Something like that?

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

      

+2


source


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

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

      

0


source







All Articles