I want to get a collection of Product entities where the product.Description property contains any of the words in a string array.
It would look something like this (result would be any product which had the word "mustard OR "pickles" OR "relish" in the Description text):
Dim products As List(Of ProductEntity) = New ProductRepository().AllProducts
Dim search As String() = {"mustard", "pickles", "relish"}
Dim result = From p In products _
Where p.Description.Contains(search) _
Select p
Return result.ToList
I already looked at this similar question but couldn't get it to work.
Since you want to see if search contains a word which is contained in the description of p you basically need to test for each value in search if it is contained in the description of p
This is c# syntax for the lambda method since my vb is not that great
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
HashSet
instead of aList
):In order to filter a collection of sentences according to this criterion all you have to do its put it into a function and call
Where
:Or put it in a lambda:
Ans From => How to check if any word in my List<string> contains in text by @R. Martinho Fernandes