I have a
List<string> words = new List<string> {"word1", "word2", "word3"};
And i want to check using linq if my string contains ANY of these words; Smthng like:
var q = myText.ContainsAny(words);
And the second, if i have a list of sentences too:
List<string> sentences = new List<string> { "sentence1 word1" , "sentence2 word2" , "sentence3 word3"};
and also neet to check if any of these sentence contains any of these words!
var q = sentences.Where(s=>words.Any(s.text))....
You can use a simple LINQ query, if all you need is to check for substrings:
If you want to check for whole words, you can use a regular expression:
Matching against a regular expression that is the disjunction of all the words:
Splitting the string into words with a regular expression, and testing for membership on the words collection (this will get faster if you use make words into a
HashSetinstead of aList):In order to filter a collection of sentences according to this criterion all you have to do is put it into a function and call
Where:Or put it in a lambda: