38

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.

Community
  • 1
  • 1
Steve Macdonald
  • 1,745
  • 2
  • 20
  • 34

3 Answers3

93

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

result = from p in products
           where search.Any(val => p.Description.Contains(val))
           select p;

This is c# syntax for the lambda method since my vb is not that great

Grizzly
  • 19,595
  • 4
  • 60
  • 78
  • 3
    Brilliant! It worked. VB syntax is: search.Any(Function(n) p.Description.ToLower.Contains(n)) – Steve Macdonald Nov 18 '09 at 16:45
  • When I try to do this, I get "Unable to create a constant value of type 'Closure type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context." my "search" is a List and my "description" is also a string. – Victor Rodrigues Jan 15 '10 at 20:01
6
Dim result = From p in products _
             Where search.Any(Function(s) p.Description.Contains(s))
             Select p
jason
  • 236,483
  • 35
  • 423
  • 525
5

You can use a simple LINQ query, if all you need is to check for substrings:

var q = words.Any(w => myText.Contains(w));
// returns true if myText == "This password1 is weak";

If you want to check for whole words, you can use a regular expression:

  1. Matching against a regular expression that is the disjunction of all the words:

    // you may need to call ToArray if you're not on .NET 4
    var escapedWords = words.Select(w => @"\b" + Regex.Escape(w) + @"\b");
    // the following line builds a regex similar to: (word1)|(word2)|(word3)
    var pattern = new Regex("(" + string.Join(")|(", escapedWords) + ")");
    var q = pattern.IsMatch(myText);
    
  2. 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 a List):

    var pattern = new Regex(@"\W");
    var q = pattern.Split(myText).Any(w => words.Contains(w));
    

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:

 // Given:
 // bool HasThoseWords(string sentence) { blah }
 var q = sentences.Where(HasThoseWords);

Or put it in a lambda:

 var q = sentences.Where(s => Regex.Split(myText, @"\W").Any(w => words.Contains(w)));

Ans From => How to check if any word in my List<string> contains in text by @R. Martinho Fernandes

Community
  • 1
  • 1
Nilesh Moradiya
  • 691
  • 1
  • 10
  • 19