57

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;
ProfK
  • 49,207
  • 121
  • 399
  • 775
  • Could you post the 'following code'? Or is the subject the complete code? You mean String.StartsWith instead of Boolean.StartsWith? – Rutger Nijlunsing Jun 13 '09 at 10:16
  • @Rutger, thanks, I was too quick to hit send and forgot the code. I also fixed the signature. – ProfK Jun 13 '09 at 10:41
  • There're at least 6 parentheses in excess, and we're still missing the function prototype and the class definition... – em70 Jun 13 '09 at 10:44
  • 3
    Try using StartsWith overload without StringComparison.CurrentCultureIgnoreCase. – Rinat Abdullin Jun 13 '09 at 11:14
  • I have not used LINQ to Entities so I don't know whether it supports wildcards, but can't you use "like" plus a wild card? – flipdoubt Jun 13 '09 at 11:36
  • @emaster: What function prototype or class definition would you like? Everything relevant to my question is present, and the surplus parentheses have no bearing on the issue. – ProfK Jun 15 '09 at 09:59
  • Related posts - [LINQ to Entities does not recognize the method](https://stackoverflow.com/q/7259567/465053) & [Entity Framework Specification Pattern Implementation](https://stackoverflow.com/q/2352764/465053) – RBT Mar 01 '19 at 11:27
  • Another related post - [LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression](https://stackoverflow.com/q/5899683/465053) – RBT Mar 01 '19 at 11:29

1 Answers1

107

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.

Vincent
  • 1,459
  • 15
  • 37
BengtBe
  • 4,465
  • 1
  • 26
  • 19