Entity Framework search records with a list of strings?

asp.net asp.net-mvc-4 entity-framework sql

Question

There are records in my database that each have a title. I want to be able to use a search term that will be divided into a list or an array to browse this database.

So, for instance, if I search for "Book Dog," it will look for any books whose names contain either "Book" or "Dog."

Using entity framework, I believe the most straightforward approach to express what I want to achieve is

string[] words;
var posts = (from p in ctx.posts
where p.title.contains(words)
select p).ToList();

The method 'Boolean ContainsAny(System.String, System.String[])' method cannot be translated into a store expression, according to the error message I would receive while attempting to use a StringExtension I discovered online.

The extension is also

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;
    }
1
2
4/26/2013 10:12:15 PM

Accepted Answer

Are you trying to find this?

var posts = (from p in ctx.posts
    where words.Any(w => p.title.Contains(w))
    select p).ToList();
10
4/26/2013 10:28:08 PM

Popular Answer

Do you need this:

ctx.posts.Where(post => words.Any(word => post.Title.Contains(word)))


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow