Problem with LINQ to Entities and String.StartsWith

.NetLinqEntity FrameworkLinq to-Entities

.Net Problem Overview


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
           where
               ((dp.LastName.StartsWith(searchTerm, StringComparison.CurrentCultureIgnoreCase)) ||
                (dp.Department.StartsWith(searchTerm, StringComparison.CurrentCultureIgnoreCase)) ||
                dp.Extension.StartsWith(searchTerm, StringComparison.CurrentCultureIgnoreCase))
           select dp;

.Net Solutions


Solution 1 - .Net

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:

dp.LastName.StartsWith(searchTerm)

or:

dp.LastName.ToLower().StartsWith(searchTerm)

and then make sure that searchTerm is also lowercase.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionProfKView Question on Stackoverflow
Solution 1 - .NetBengtBeView Answer on Stackoverflow