.net – Problem with LINQ to Entities and String.StartsWith


I'm trying to build a search page using LINQ to Entities, but the following code is giving me a runtime error about l.t.e. not recognising 'Boolean StartsWith(). The code compiles just fine. How can I work around this better than shipping the StartsWith filtering out to a stored proc?

    return from dp in dents.DirectoryPersonEntrySet
               ((dp.LastName.StartsWith(searchTerm, StringComparison.CurrentCultureIgnoreCase)) ||
                (dp.Department.StartsWith(searchTerm, StringComparison.CurrentCultureIgnoreCase)) ||
                dp.Extension.StartsWith(searchTerm, StringComparison.CurrentCultureIgnoreCase))
           select dp;

Best Solution

I would guess that EF doesn't support the overload of StartsWith that takes a StringComparison parameter.

It should support StartsWith, EndsWith and Contains, so maybe you can try:




and then make sure that searchTerm is also lowercase.