LINQ to Entities and String have a problem. StartsWith

.net entity-framework linq linq-to-entities

Question

I'm attempting to use LINQ to Entities to create a search page, however the following code is throwing a runtime error about l.t.e. not understanding "Boolean StartsWith" (). The code successfully builds. What alternative to transporting the StartsWith filtering out to a stored proc can I use to get around this?

    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;
1
49
6/13/2009 10:40:27 AM

Accepted Answer

The overload of StartsWith that accepts a StringComparison argument is probably not supported by EF.

StartsWith, EndsWith, and Contains should all be supported, so maybe try:

dp.LastName.StartsWith(searchTerm)

or:

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

after which, ensure thatsearchTerm is lowercase as well.

94
11/7/2014 1:37:01 PM


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