Perform string search with Linq to Entities

.net, c#, linq, linq-to-entities, string

Solution

First, it's tricky (if possible which I don't know) to use StringComprison in Expressions and expect Linq 2 Entities to translate it to correct Sql statements.

Second, it's tricky to use a custom function like your ContainsAny in an Expression too.

So if I were you, the simple solution is:

GetContacts().Where(c => searchTerms.Any(term => c.FullName.Contains(term)))

which should work in EF4.

Problem

I want to enable this function in Linq to Entities (so the filtering happens on the SQL Server)? ``` public static bool ContainsAny(this string source, StringComparison comparison, IEnumerable<string> searchTerms) { return searchTerms.Any(searchTerm => source.Contains(searchTerm, comparison)); } ``` My goal is search a table and limit the result by filtering a certain column with the above function, i.e. `GetContacts().Where(c => c.FullName.ContainAny(searchTerm))`.

Original source