there are two ways this can be done, the first is with a loop, the second is using LINQ’s “Any” extension method with 1 line of code and a lambda expression:
goldensum’s post made me re-read the question (sorry!). I wrote it in reverse. I think what he was trying to say is “does the string contain any of the items in the array”. Therefore the needles and the haystack are reversed. Here are the two options:
In cases like this, the linq bit will still be doing a loop under the covers – how else can it find the match where there is no index or other way of cheating?
Yes, of course. But somehow I still prefer the elegance of LINQ and though this is a simple example I find it pays dividends in other areas where one ends up using a cleaner more functional programming style rather than mutating variables.
To take it to the extreme, I occasionally use the .ForEach(x => DoSomething(x)) extension on my List<T> rather than a foreach(var x in y) { DoSomething(x); } I note ReSharper seems to prefer this too). Then when you want to parallelize it, it’s as simple as using the Parallel.ForEach method rather than the ForEach extension method. If I want to do a transformation of the data beforehand, I can just use a .Select(x => x) extension method in front of the .ForEach().
No arguments there. Just wanted to dispel the idea that linq was some sort of database like magic that could make straight list lookups faster than O(n) operations . . .