views:

943

answers:

3

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.

+1  A: 
Dim result = From p in products _
             Where search.Any(Function(s) p.Description.Contains(s))
             Select p
Jason
Damn you stole my first ever VB.NET answer! ;p +1 anyways
leppie
@leppie: Yeah, this is one of my few VB.NET answers.
Jason
A: 

You have it the wrong way around :)

It should be:

Where search.Contains(p.Description)
leppie
No, description should CONTAIN any element of search as a substring, not actually be one of them.
Victor Rodrigues
+2  A: 

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
Brilliant! It worked. VB syntax is:search.Any(Function(n) p.Description.ToLower.Contains(n))
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<string> and my "description" is also a string.
Victor Rodrigues