I have a database of records that each have a title. I want to be able to search through this database with a search string that will be separated into a list or an array.
So for example if I search with "Book Dog", it will search for all titles that have "Book" or "Dog" in the title.
I'm using entity framework and I guess the simplest way to write down what I want to do is
string[] words;
var posts = (from p in ctx.posts
where p.title.contains(words)
select p).ToList();
I've tried using a StringExtension I found online, but I would get the following error "LINQ to Entities does not recognize the method 'Boolean ContainsAny(System.String, System.String[])' method, and this method cannot be translated into a store expression."
And the extension is
public static bool ContainsAny(this string str, params string[] values)
{
if (!string.IsNullOrEmpty(str) || values.Length > 0)
{
foreach (string value in values)
{
if (str.Contains(value))
return true;
}
}
return false;
}