How would I reverse contains in a Linq-to-SQL query so that I can check if Title
or Description
contain any word from my list, something like:
var query = context.Shapes.Where(x => x.Title.Contains(words));
Here is what I have now but that is opposite from what I need.
List<string> words = Search.GetTags(q);
//words = round,circle,square
using(ShapesDataContext context = new ShapesDataContext())
{
var query = context.Shapes.Where(x => words.Contains(x.Title) ||
words.Contains(x.Description));
}
// Item 1: Title = Elipse , Decsription = This is not round circle
//This should be a match! but words doesn't contain
//"This is not round circle", only round and circle so no match
UPDATE
Now I have
var query = context.Shapes.Where(x => words.Any(w => x.Title.Contains(w) || x.Description.Contains(w)))
int s = query.Count();
but now I get exception on int s = query.Count();
with message "Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator." Does anyone know how to solve it?
are you looking for something like NOT-IN collection query?
Then this blog post might help
http://introducinglinq.com/blogs/marcorusso/archive/2008/01/14/the-not-in-clause-in-linq-to-sql.aspx
HTH
my solution is using sub query ( sub select)
You want
Not the most efficient but I managed: