Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Result of LINQ.Any to string

So, I check if a string contains a certain word from an array with the following statement:

if(stringArray.Any(s => stringToCheck.Contains(s)))

Simple. Now a match is found and the statement is true. But I want to know also which item in the array matched with the string. The placeholder "s" in the LINQ statement above is not available in the following clause.

Hope somebody has an idea. I could just loop through the array, yes, but LINQ looks way nicer to me. =)

like image 842
Neurodefekt Avatar asked Dec 21 '22 12:12

Neurodefekt


1 Answers

var match = stringArray.FirstOrDefault(s => stringToCheck.Contains(s));
if(match != null) {
    // match was found
}
like image 196
Christian Hayter Avatar answered Dec 23 '22 03:12

Christian Hayter