Sql – Linq to Sql Criteria in String Array

linq-to-sql

I have say three tables.

Projects

  • Id
  • Name

Categories

  • Id
  • Name

ProjectCategories

  • Id
  • ProjectId
  • CategoryId

I now have an array of strings which represents categories the user has elected to search on. I now need to find all the projects that have those category attached to them.

I tried;

string[] searchTerms = new string[2];
searchTerms[0] = "paint";
searchTerms[1] = "painting";

IQueryable<Project> projects = (from category in dc.ProjectCategories
where searchTerms.Any(val => category.Category.Name.Contains(val))
select category.Project).Distinct();

But I get the error;

{"Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator."}

Best Answer

Unless I'm mistaking your meaning, which is possible, I think you can just do a contains with the searchTerms

where searchTerms.Contains( category.Category.Name )

this will translate into SQL as

... WHERE Name IN ('paint','painting') ...