Reputation: 24488
I have built a search feature for a product site. The search works fine. Recently, we have added a SearchTerm field in the database. SearchTerm Data example: "work shoes blue black gear" Current code is
pM = (from p in ctx.Products
where
p.productSearchField.Contains(term) ||
p.productName.Contains(term)
select p).ToList()
If the term = "shoes" <-- works
If the term = "work shoes" <-- works
If the term = "black shoes" <-- does NOT work.
I also tried to separate the SearchTerm Data by commas, but that did not work. Any advice?
Upvotes: 3
Views: 108
Reputation: 82584
Split the term into single terms. See if any match:
term.Split(' ').Any(i => p.productSearchField.Contains(i))
Or if every word has to match:
term.Split(' ').All(i => p.productSearchField.Contains(i))
var terms = term.Split(' ');
pM = (from p in ctx.Products
where
terms.All(i => p.productSearchField.Contains(i)) ||
p.productName.Contains(term)
select p).ToList()
Upvotes: 3